Quantcast
Channel: SQLServerCentral » SQL Server 2012 » SQL Server 2012 - T-SQL » Latest topics
Viewing all 4901 articles
Browse latest View live

Doubt on trigger

$
0
0
Hello Everybody, I have a table and sp . I want no one to drop those. Am I able to set instead of trigger for this. Else is there any other way to achieve that.Thanks in advance

Help with SQL Maths Operators

$
0
0
HelloI have a calculation in a Access Database that I am trying to change into a SQL Server query; however, I'm getting a error on one of the Maths Operators (^) used. I have looks on MSDN and this seems to be included so I'm unsure why this isn't working.Below is the SQL[code="sql"]SELECT ,MTG_QCA = CASE WHEN ttss_avgeqcapointscore< 1 THEN 0 ELSE Round([LAT2011].[Gamma0]+[LAT2011].[Gamma1]*[ttss_avgeqcapointscore]+[LAT2011].[Gamma2]*[ttss_avgeqcapointscore]^2+[LAT2011].[Gamma3]*[ttss_avgeqcapointscore]^3+[LAT2011].[Gamma4]*[ttss_avgeqcapointscore]^4,2) END[/code]This is the error message that I recieveMsg 402, Level 16, State 1, Line 9The data types numeric and int are incompatible in the '^' operator.Any help would be much appreciated :-)

Simple BCP will not work on clusters

$
0
0
I have a fairly simple need to place a text file on in a directory from a query. BCP seemed the best way to do this, and my simple script works fine except on non-clustered instances. Here is the script:Declare @cmd varchar(2048), @path varchar(100)select @path = (select Path from [Path] where Number = (select max(Number) from [Path])) + 'version.txt'SET @CMD = 'bcp "select @@version" queryout ' + @path + ' -SLOCALHOST -E -T -c'exec master..xp_cmdshell @cmd, NO_OUTPUTThis works fine on my non-clustered instances. On my clustered instances I get a time out erroroutput--------------------------------------------------------------------------------------------------SQLState = 08001, NativeError = 2Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. SQLState = 08001, NativeError = 2Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.SQLState = S1T00, NativeError = 0Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expiredNULLAnd yes, remote connections are enabled. I am thinking is has something to do with the -S parameter and virtual (not VM) names with clustering. But I can't figure it out.Any ideas?

Deletes takes a long time, but CPU, Disk and RAM untroubled...why ?

$
0
0
Hi all,Running SQL 2012 BI on a spanky new server with 2690 x 2 CPU, fast SSD, ramdisk for tempdb and 128GB RAM. I have a problem finding a performance bottleneck, in essence.Deleting records on a where condition is a the simplest example, but some select queries exhibit this behaviour too, for part of their runtime. Back to the delete example!If CPU idles at 3 or 4% whilst the delete is running.And RAM use is 13GB of 128GBAnd Disk is idling at a few K per secondWhy does this delete operation take minutes ? Why wouldn't SQL use more resources to get the job done faster ?Regards, Greg.

sales by product category

$
0
0
Return the number of sales by product category where the average recommended price isR10 or more greater than the average sales price.

how to update column city value from 'A' to 'B' and 'B' to 'A' in single query

$
0
0
hi,I have a table like id city1 Aso i want to update city column from A to B and again B to A by in single statement..please help.ThanksDastagiri

FULLTEXT problem

$
0
0
Hi,I've a table with a fulltext index and when I search for an exact phrase I get a result but when I search for all the words with AND operator I get no results.Is there something wrong with my fulltext?[code="sql"]SELECT * FROM CONTAINSTABLE(dbo.KNB_ItemCultures, *, '"How to migrate reports between different versions"')-- get 1 resultSELECT * FROM CONTAINSTABLE(dbo.KNB_ItemCultures, *, '"How" AND "to" AND "migrate" AND "reports" AND "between" AND "different" AND "versions"')-- no resultsSELECT * FROM CONTAINSTABLE(dbo.KNB_ItemCultures, *, '"*How*" AND "*to*" AND "*migrate*" AND "*reports*" AND "*between*" AND "*different*" AND "*versions*"')-- no results either[/code]Thanks,Pedro

Query now runs forever

$
0
0
Any idea how this might run forever? It is running through 174,000 rows, but that usually takes sub-second. Would this indicated corruption of some sort? I've made no changes to the query (as a view...) in weeks. "AllLaborInput" is imported weekly...yesterday.[code="sql"] SELECT [tblWorkOrders].[ID] AS WOID , Sum(round([Reg Hrs], 2)) AS StdHrs , Sum(round([OT Hrs], 2)) AS OvtHrs , Sum((CAST(round([Line Cost], 2) AS money))) AS InternalLaborCost FROM ([AllLaborInput] INNER JOIN [tblWorkOrders] ON [AllLaborInput].[WO #] = [tblWorkOrders].[WONbr]) LEFT JOIN [tblUsers] ON [AllLaborInput].[WORKER NO] = [tblUsers].[UserID] WHERE ((([AllLaborInput].[VENDOR NAME]) IS NULL) AND (([tblUsers].[UserLaborExclusion]) = 0 OR ([tblUsers].[UserLaborExclusion]) IS NULL)) OR ( (([AllLaborInput].[VENDOR NAME]) IS NULL) AND (([AllLaborInput].[START DATE]) > CONVERT(DATETIME, '2010-07-12T00:00:00.000', 126)) AND (([tblUsers].[UserPassword]) = 'nfox') ) GROUP BY [tblWorkOrders].[ID][/code]

SQL 2012 ERROR - The metadata could not be determined because statement contains dynamic SQL

$
0
0
Hi, This post is related to an error I have discovered through the execution of a SSIS package that has been migrated and upgraded in a SQL Server 2012 instance. The package has failed in initial testing in 2012 and the root error/issue seems to be this message:sg 11514, Level 16, State 1, Procedure sp_describe_first_result_set, Line 1The metadata could not be determined because statement 'EXEC sp_executesql @Sql, @ParamDetails, @IdParam = @Id, @Sta' in procedure 'PROCNAMECHANGEDTOPROTECTTHEGUILTY' contains dynamic SQL. Consider using the WITH RESULT SETS clause to explicitly describe the result set. I am not the author of the package, so I don't know the full details of what it does but in the end it calls a proc that generates and executes dynamic SQL. And it runs fine in our 2008R2 instances. I have been able to track a similar message related to an inability to determine metadata when referencing a temp table, but I have not come across a message in my searches that specifically mention metadata not being determined due to a proc executing dynamic SQL as noted. Not really expecting a fix (although that would be sweet!) from you folks, I'm hopeful that some discussion might be sparked around functional changes related to gathering metadata in 2012 that are contributing to the problem because I frankly don't get what's happening and I would like to gather as much information as possible to troubleshoot further. So has anyone seen this message in the course of migrating to 2012? Any insight, online references, or experiences are much appreciated.D

Problem with writing a query. Kindly guide...

$
0
0
Hi All, I have RATE and SHIFT columns in a table as mentioned below. When I pass RATE=1 then I get SHIFT = 1,2,3. Now in second step, I need to write a query that will find all rates where atleast these three shifts compulsorily exist (e.g. RATE 3,5 as per below data). All above work is to be done as a part of a single query and first input of RATE will be dynamic. Below mentioned data is only for example, and real table will be having huge data.Rate Shift1 11 21 32 53 13 23 38 44 14 25 15 25 35 46 17 28 2Regards,Ashish

Best way to check record existence and get its data

$
0
0
Hi,What the best way to check if a record exists and at get a column from it to use?[code="sql"]DECLARE @order INT = '1'DECLARE @col INTIF EXISTS (SELECT 1 FROM Orders WHERE NumOrder = @order)BEGIN SELECT @col = col FROM Orders WHERE NumOrder = @order ......END[/code]Or[code="sql"]DECLARE @order INT = '1'DECLARE @col INTSELECT @col = col FROM Orders WHERE NumOrder = @orderIF @@ROWCOUNT > 0BEGIN ......END[/code]Or is there another way?[code="sql"]IF EXISTS (SELECT @col = col FROM Orders WHERE NumOrder = @order) [/code] doesn't work :-(Thanks,Pedro

What does *= mean in a Select statement?

$
0
0
I'm no genius, and I've just taken over a System that has many more stored procedures than I like. Some of these stored procedures contain the following line in a SELECT statement, which SQL SERVER 2012 does NOT like - I have no idea what it means. Can anyone help?And BgnDate *= ProcPer

Conditional Default value for a column

$
0
0
Dear AllWhant to assign conditional default value during table creation Likecreate table aa( cc int null ,bb int default (case when cc = 1 then 1 else 2 end )not null)Is it possible?

Query reports duplicates when running a GROUP BY but not when I try to see the actual records?

$
0
0
I have a query that joins several tables and I am trying to identify duplicates, so I am running some COUNT/GROUP BY/HAVING queries on various fields like User ID, Last Name + First Name + Middle Name and SSN. When I run it on SSN I get two SELECT Ssn, COUNT(*)FROM tables and joinsWHERE clauses, ...GROUP BY SsnHAVING COUNT(*) > 1ORDER BY SsnProduces:Ssn Count1234567890 20123456789 2But when I try to see the records in question using the exact same FROM/JOINS/WHERE clause, they don't appear to actually be present in the data:SELECT *FROM tables and joinsWHERE clauses, ...AND Ssn IN ('1234567890', '0123456789')Produces no records.Is this possibly a bad index???Thanks in advance!!!

UDF - IF(@parameter)

$
0
0
HelloIs it possible to build an udf that accepts 3 parameters(@a,@b, @c) and, if @a is null (build cte1) else build cte2? I always get an error at the ELSE (incorrect syntax)EX:IF (@User_RefID IS NULL) WITH cte AS (SELECT DISTINCT..[...].)ELSE WITH cte AS (SELECT DISTINCT..[...].)INSERT INTO .....the function is supposed to return a table RETURNS @MonthlyReport TABLE ( <columns>>AS BEGIN

sp_OAMethod to retrieve the file attributes from a folder

$
0
0
Hi,I found the below code that will bring back "some" of the attributes of any file in the selected folder... however, I also need the Created Date of the file... I get the modify date, I need both.In the code, I added "CreateDate", it runs, but brings back less information than when I have it commented out and the created date is null...Does anyone have any ideas as to what I should do???Thanks,John[code="sql"]SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================alter FUNCTION [dbo].[Dir](@Wildcard VARCHAR(8000))/* returns a table representing all the items in a folder. It takes as parameter the path to the folder. It does not take wildcards in the same way as a DIR command. Instead, you would be expected to filter the results of the function using SQL commandsNotice that the size of the item (e.g. file) is not returned by this function. This function uses the Windows Shell COM object via OLE automation. It opens a folder and iterates though the items listing their relevant properties. You can use the SHELL object to do all manner of things such as printing, copying, and moving filesystem objects, accessing the registry and so on. Powerful medicine.--e.g.--list all subdirectories directories beginning with M from "c:\program files"SELECT [path] FROM dbo.dir('c:\program files') WHERE name LIKE 'm%' AND IsFolder =1SELECT * FROM dbo.dir('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG')*/RETURNS @MyDir TABLE ( -- columns returned by the function [name] VARCHAR(2000), --the name of the filesystem object [path] VARCHAR(2000), --Contains the item's full path and name. [ModifyDate] DATETIME, --the time it was last modified --[CreatedDate] DATETIME, [IsFileSystem] INT, --1 if it is part of the file system [IsFolder] INT, --1 if it is a folsdder otherwise 0 [error] VARCHAR(2000) --if an error occured, gives the error otherwise null)AS-- body of the functionBEGIN DECLARE --all the objects used @objShellApplication INT, @objFolder INT, @objItem INT, @objErrorObject INT, @objFolderItems INT, --potential error message shows where error occurred. @strErrorMessage VARCHAR(1000), --command sent to OLE automation @Command VARCHAR(1000), @hr INT, --OLE result (0 if OK) @count INT,@ii INT, @name VARCHAR(2000),--the name of the current item @path VARCHAR(2000),--the path of the current item @ModifyDate DATETIME,--the date the current item last modified --@CreatedDate DATETIME, @IsFileSystem INT, --1 if the current item is part of the file system @IsFolder INT --1 if the current item is a file IF LEN(COALESCE(@Wildcard,''))<2 RETURN SELECT @strErrorMessage = 'opening the Shell Application Object' EXECUTE @hr = sp_OACreate 'Shell.Application', @objShellApplication OUT --now we get the folder. IF @HR = 0 SELECT @objErrorObject = @objShellApplication, @strErrorMessage = 'Getting Folder"' + @wildcard + '"', @command = 'NameSpace("'+@wildcard+'")' IF @HR = 0 EXECUTE @hr = sp_OAMethod @objShellApplication, @command, @objFolder OUT IF @objFolder IS NULL RETURN --nothing there. Sod the error message --and then the number of objects in the folder SELECT @objErrorObject = @objFolder, @strErrorMessage = 'Getting count of Folder items in "' + @wildcard + '"', @command = 'Items.Count' IF @HR = 0 EXECUTE @hr = sp_OAMethod @objfolder, @command, @count OUT IF @HR = 0 --now get the FolderItems collection SELECT @objErrorObject = @objFolder, @strErrorMessage = ' getting folderitems', @command='items()' IF @HR = 0 EXECUTE @hr = sp_OAMethod @objFolder, @command, @objFolderItems OUTPUT SELECT @ii = 0 WHILE @hr = 0 AND @ii< @count --iterate through the FolderItems collection BEGIN IF @HR = 0 SELECT @objErrorObject = @objFolderItems, @strErrorMessage = ' getting folder item ' + CAST(@ii AS VARCHAR(5)), @command='item(' + CAST(@ii AS VARCHAR(5))+')' --@Command='GetDetailsOf('+ cast(@ii as varchar(5))+',1)' IF @HR = 0 EXECUTE @hr = sp_OAMethod @objFolderItems, @command, @objItem OUTPUT IF @HR = 0 SELECT @objErrorObject = @objItem, @strErrorMessage = ' getting folder item properties' + CAST(@ii AS VARCHAR(5)) IF @HR = 0 EXECUTE @hr = sp_OAMethod @objItem, 'path', @path OUTPUT IF @HR = 0 EXECUTE @hr = sp_OAMethod @objItem, 'name', @name OUTPUT IF @HR = 0 EXECUTE @hr = sp_OAMethod @objItem, 'ModifyDate', @ModifyDate OUTPUT --IF @HR = 0 -- EXECUTE @hr = sp_OAMethod @objItem, -- 'CreateDate', @CreatedDate OUTPUT IF @HR = 0 EXECUTE @hr = sp_OAMethod @objItem, 'IsFileSystem', @IsFileSystem OUTPUT IF @HR = 0 EXECUTE @hr = sp_OAMethod @objItem, 'IsFolder', @IsFolder OUTPUT --and insert the properties into a table INSERT INTO @MyDir ([NAME], [path], ModifyDate, IsFileSystem, IsFolder) --CreatedDate, SELECT @NAME, @path, @ModifyDate, @IsFileSystem, @IsFolder --@CreatedDate, IF @HR = 0 EXECUTE sp_OADestroy @objItem SELECT @ii=@ii+1 END IF @hr <> 0 BEGIN DECLARE @Source VARCHAR(255), @Description VARCHAR(255), @Helpfile VARCHAR(255), @HelpID INT EXECUTE sp_OAGetErrorInfo @objErrorObject, @source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT SELECT @strErrorMessage = 'Error whilst ' + COALESCE(@strErrorMessage, 'doing something') + ', ' + COALESCE(@Description, '') INSERT INTO @MyDir(error) SELECT LEFT(@strErrorMessage,2000) END EXECUTE sp_OADestroy @objFolder EXECUTE sp_OADestroy @objShellApplicationRETURNENDGO[/code]

Pattern match any 3 repeating letters

$
0
0
Is there a more concise way than something like this? [code="sql"]or name like 'aaa%'or name like 'bbb%'or name like 'ccc%'or name like 'ddd%'or name like 'eee%'or name like 'fff%'or name like 'ggg%'or name like 'hhh%'or name like 'iii%'or name like 'jjj%'or name like 'kkk%'or name like 'lll%'or name like 'mmm%'or name like 'nnn%'or name like 'ooo%'or name like 'ppp%'or name like 'qqq%'or name like 'rrr%'or name like 'sss%'or name like 'ttt%'or name like 'uuu%'or name like 'vvv%'or name like 'www%'or name like 'xxx%'or name like 'yyy%'or name like 'zzz%'[/code]Thanks

display order by like 1,2,3,4,5...............plz write quarie

$
0
0
Examples of values I want to put in orderhouseno3-13-13-13-3/a3-13-3/b3-13-2/a3-13-43-13-63-13-5/a3-13-4/ci want output like this belowhouseno3-13-13-13-2/a3-13-3/a3-13-3/b3-13-43-13-4/c3-13-5/a3-13-6

create a view to show the backup status in every 10 mins

$
0
0
I have a question,my table have following data:userID, startTime, EndTime—————————————101, 04/11/2013 11:00:00, 04/11/2013 11:55:00102, 04/11/2013 11:00:00, 04/11/2013 11:24:00103, 04/11/2013 11:20:00, 04/11/2013 11:45:00104, 04/11/2013 11:30:00, 04/11/2013 11:35:00105, 04/11/2013 11:40:00, 04/11/2013 11:55:00can I use the view to show the backup status in every 10 mins?I wonder the result as following:time, count——————————04/11/2013 11:00:00, 204/11/2013 11:10:00, 204/11/2013 11:20:00, 304/11/2013 11:30:00, 304/11/2013 11:40:00, 304/11/2013 11:50:00, 204/11/2013 12:00:00, 004/11/2013 11:00:00 – 04/11/2013 11:09:59 have 2 jobs, 101 & 10204/11/2013 11:10:00 – 04/11/2013 11:19:59 have 2 jobs, 101 & 10204/11/2013 11:20:00 – 04/11/2013 11:29:59 have 3 jobs, 101 & 102 & 103…04/11/2013 11:50:00 – 04/11/2013 11:59:59 have 2 jobs, 101 & 10504/11/2013 12:00:00 – 04/11/2013 12:09:59 have 0 jobI wonder if you can give me a help……thanks a lot

Issue with View - Error

$
0
0
Hi there, I am currently looking at someone else code and am getting an error when I try and run the view. When I run the code directly in Management Studio it works with now errors - really confused.The script is - [code="sql"]SELECT TEN.[tenancy-ref] AS 'Tenancy Reference' ,CASE WHEN TEN.[curr-balance] >0 THEN TEN.[curr-balance] ELSE 0 END AS 'Arrears' ,TEN.[curr-balance]+ TEN.[hb-arrears] AS 'Arrears Inc HB' ,TEN.[curr-balance]+ TEN.[hb-arrears] + TEN.[sp-arrears]AS 'Arrears Inc HB and SP' ,TEN.[net-rent] AS 'Net Rent' ,TEN.[pmt-method] AS 'Payment Method' ,TEN.[tncy-status] AS 'Tenancy Status' ,TEN.[corr-name1] AS 'Tenant Name' ,PLAC.[address1] AS 'Address 1' ,PLAC.[address2] AS 'Address 2' ,PLAC.[address3] AS 'Address 3' ,PLAC.[address4] AS 'Address 4' ,PLAC.[address5] AS 'Address 5' ,PLAC.[post-code] AS 'PostCode' ,CASE WHEN TEN.[curr-balance] <0 THEN TEN.[curr-balance] ELSE 0 END AS 'PrePaid' ,TEN.[rent-group] AS 'Rent Group' ,TEN.[tncy-type] AS 'Tenancy Type' ,CASE WHEN TEN.[tncy-status]= 'FOR' THEN LOC.[former-arrs-ofcr] ELSE LOC.[arrears-ofcr]END AS 'Arrears Officer' ,OFFI.[OFCR-TITLE]+ ' '+ INITIALS + ' ' + [SURNAME] AS 'Arrears Officer Name' ,PLAC.[parish-code]AS 'Parish Code' ,LOC.[bedrooms] AS 'Bedrooms' ,LOC.[location-type] AS 'Location Type' ,LOC.[building-type] AS 'Building Type' ,LOC.[mgt-area]AS 'Management Area' ,LOC.[scheme] AS 'Scheme' ,LOC.[accounts-company]AS 'Accounts Company' ,TEN.[gross-rent] AS 'Gross Rent' ,TEN.[tncy-start] AS 'Tenancy StartDate' ,TEN.[tncy-end] AS 'Tenancy EndDate' ,CASE WHEN TEN.[curr-balance]+ TEN.[hb-arrears] <0 THEN 0 ELSE TEN.[curr-balance]+ TEN.[hb-arrears]END AS 'Arrears Inc HB Excluding Credits' ,CASE WHEN TEN.[curr-balance]+ TEN.[hb-arrears]+ TEN.[sp-arrears] <0 THEN 0 ELSE TEN.[curr-balance]+ TEN.[hb-arrears]+ TEN.[sp-arrears]END AS 'Arrears Inc HB and SP Excluding Credits' ,TEN.[tncy-cat] AS 'Tenancy Category' ,LOC.[former-arrs-ofcr] AS 'Former Arrears Officer' /* added by Mandy 19-11-12 ----------------------------*/ ,TEN.[arrears-stage] AS 'Arrears Stage' ,CASE WHEN (TEN.[net-rent]<=0 OR TEN.[net-rent] IS NULL OR (TEN.[curr-balance]+ TEN.[hb-arrears] + TEN.[sp-arrears]<=0)) THEN 0 ELSE (TEN.[curr-balance]+ TEN.[hb-arrears] + TEN.[sp-arrears])/TEN.[net-rent] END AS 'Net Arrears Div By Net Rent' ,SUM(TRAS6.[TRANS-AMT]) AS 'Week6' ,SUM(TRAS5.[TRANS-AMT]) AS 'Week5' ,SUM(TRAS4.[TRANS-AMT]) AS 'Week4' ,SUM(TRAS3.[TRANS-AMT]) AS 'Week3' ,SUM(TRAS2.[TRANS-AMT]) AS 'Week2' ,SUM(TRAS1.[TRANS-AMT]) AS 'Week1' /* ---------------------------------------------------*/FROM dbo.[IH_RE-TENANCY] AS TEN INNER JOIN dbo.[IH_RE-TNCY-PLACE] AS TENPLAC ON TEN.[tncy-sys-ref] = TENPLAC.[tncy-sys-ref] INNER JOIN dbo.[IH_IH-LOCATION] AS LOC ON TENPLAC.[place-ref] = LOC.[place-ref] INNER JOIN dbo.[CORE_CO-PLACE]AS PLAC ON LOC.[place-ref] = PLAC.[place-ref] LEFT OUTER JOIN dbo.[IH_OFFICER] AS OFFI ON LOC.[arrears-ofcr] = OFFI.[OFFICER-CODE] ---Week Join - Week 6 Added by Mandy 19-11-12-------- LEFT OUTER JOIN dbo.[IH_RE-TNCY-TRANS] AS TRAS6 ON TRAS6.[TNCY-SYS-REF]=TEN.[tncy-sys-ref] AND TRAS6.[TRANS-WEEK]= ( SELECT MAX(WK.[TRASWEEK]) FROM ( SELECT DISTINCT TRA6.[TRANS-WEEK] AS 'TRASWEEK' FROM dbo.[IH_RE-TNCY-TRANS] AS TRA6 WHERE TRA6.[TRANS-DATE]>GETDATE()-42 AND TRA6.[ACCOUNT-TYPE]='IN' GROUP BY TRA6.[TRANS-WEEK] ) AS WK ) AND TRAS6.[ACCOUNT-TYPE]='IN' AND TRAS6.[TRANS-DATE]>GETDATE()-42--------------------------------------Week Join - Week 5 Added by Mandy 19-11-12-------- LEFT OUTER JOIN dbo.[IH_RE-TNCY-TRANS] AS TRAS5 ON TRAS5.[TNCY-SYS-REF]=TEN.[tncy-sys-ref] AND TRAS5.[TRANS-WEEK]= ( SELECT MAX(WK.[TRASWEEK])-1 FROM ( SELECT DISTINCT TRA5.[TRANS-WEEK] AS 'TRASWEEK' FROM dbo.[IH_RE-TNCY-TRANS] AS TRA5 WHERE TRA5.[TRANS-DATE]>GETDATE()-42 AND TRA5.[ACCOUNT-TYPE]='IN' GROUP BY TRA5.[TRANS-WEEK] ) AS WK ) AND TRAS5.[ACCOUNT-TYPE]='IN' AND TRAS5.[TRANS-DATE]>GETDATE()-42--------------------------------------Week Join - Week 4- Added by Mandy 19-11-12------- LEFT OUTER JOIN dbo.[IH_RE-TNCY-TRANS] AS TRAS4 ON TRAS4.[TNCY-SYS-REF]=TEN.[tncy-sys-ref] AND TRAS4.[TRANS-WEEK]= ( SELECT MAX(WK.[TRASWEEK])-2 FROM ( SELECT DISTINCT TRA4.[TRANS-WEEK] AS 'TRASWEEK' FROM dbo.[IH_RE-TNCY-TRANS] AS TRA4 WHERE TRA4.[TRANS-DATE]>GETDATE()-42 AND TRA4.[ACCOUNT-TYPE]='IN' GROUP BY TRA4.[TRANS-WEEK] ) AS WK ) AND TRAS4.[ACCOUNT-TYPE]='IN' AND TRAS4.[TRANS-DATE]>GETDATE()-42--------------------------------------Week Join - Week 3- Added by Mandy 19-11-12------- LEFT OUTER JOIN dbo.[IH_RE-TNCY-TRANS] AS TRAS3 ON TRAS3.[TNCY-SYS-REF]=TEN.[tncy-sys-ref] AND TRAS3.[TRANS-WEEK]= ( SELECT MAX(WK.[TRASWEEK])-3 FROM ( SELECT DISTINCT TRA3.[TRANS-WEEK] AS 'TRASWEEK' FROM dbo.[IH_RE-TNCY-TRANS] AS TRA3 WHERE TRA3.[TRANS-DATE]>GETDATE()-42 AND TRA3.[ACCOUNT-TYPE]='IN' GROUP BY TRA3.[TRANS-WEEK] ) AS WK ) AND TRAS3.[ACCOUNT-TYPE]='IN' AND TRAS3.[TRANS-DATE]>GETDATE()-42--------------------------------------Week Join - Week 2-Added by Mandy 19-11-12 ------- LEFT OUTER JOIN dbo.[IH_RE-TNCY-TRANS] AS TRAS2 ON TRAS2.[TNCY-SYS-REF]=TEN.[tncy-sys-ref] AND TRAS2.[TRANS-WEEK]= ( SELECT MAX(WK.[TRASWEEK])-4 FROM ( SELECT DISTINCT TRA2.[TRANS-WEEK] AS 'TRASWEEK' FROM dbo.[IH_RE-TNCY-TRANS] AS TRA2 WHERE TRA2.[TRANS-DATE]>GETDATE()-42 AND TRA2.[ACCOUNT-TYPE]='IN' GROUP BY TRA2.[TRANS-WEEK] ) AS WK ) AND TRAS2.[ACCOUNT-TYPE]='IN' AND TRAS2.[TRANS-DATE]>GETDATE()-42--------------------------------------Week Join - Week 1-Added by Mandy 19-11-12 ------- LEFT OUTER JOIN dbo.[IH_RE-TNCY-TRANS] AS TRAS1 ON TRAS1.[TNCY-SYS-REF]=TEN.[tncy-sys-ref] AND TRAS1.[TRANS-WEEK]= ( SELECT MAX(WK.[TRASWEEK])-5 FROM ( SELECT DISTINCT TRA1.[TRANS-WEEK] AS 'TRASWEEK' FROM dbo.[IH_RE-TNCY-TRANS] AS TRA1 WHERE TRA1.[TRANS-DATE]>GETDATE()-42 AND TRA1.[ACCOUNT-TYPE]='IN' GROUP BY TRA1.[TRANS-WEEK] ) AS WK ) AND TRAS1.[ACCOUNT-TYPE]='IN' AND TRAS1.[TRANS-DATE]>GETDATE()-42-----------------------------------WHERE TENPLAC.[prime-place] = '1'GROUP BY TEN.[tenancy-ref] ,TEN.[pmt-method] ,TEN.[curr-balance],TEN.[tncy-status] ,LOC.[arrears-ofcr],LOC.[scheme],LOC.[mgt-area],TEN.[corr-name1] ,PLAC.[address1],PLAC.[address2],PLAC.[address3],PLAC.[address4],PLAC.[address5] ,PLAC.[post-code],LOC.[mgt-area],TEN.[tncy-cat],TEN.[tncy-type] ,TEN.[hb-arrears],TEN.[sp-arrears],TEN.[net-rent] ,TEN.[gross-rent],TEN.[rent-group],LOC.[former-arrs-ofcr] ,OFFI.[OFCR-TITLE],OFFI.[INITIALS],OFFI.[SURNAME],PLAC.[parish-code],LOC.[bedrooms],LOC.[location-type] ,LOC.[building-type],LOC.[accounts-company],TEN.[tncy-start],TEN.[tncy-end],TEN.[arrears-stage][/code]When I save this as a view and then run the view or try and attach it to a source in ssis I get the error - [i]Error Message: Conversion failed when converting the varchar value 'TRASWEEK to data type int[/i]As soon as I take the following out the SELECT statement - the view works - [code="sql"] ,SUM(TRAS6.[TRANS-AMT]) AS 'Week6' ,SUM(TRAS5.[TRANS-AMT]) AS 'Week5' ,SUM(TRAS4.[TRANS-AMT]) AS 'Week4' ,SUM(TRAS3.[TRANS-AMT]) AS 'Week3' ,SUM(TRAS2.[TRANS-AMT]) AS 'Week2' ,SUM(TRAS1.[TRANS-AMT]) AS 'Week1'[/code]Can some one please advise as I'm totally at a loss. Thanks
Viewing all 4901 articles
Browse latest View live