Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Using with in SQL Server Function

$
0
0
Why this doesn't work?I have to use code 2 as my atual scenerio is more complex as it brings data from multiple queries and should filter it in with clause before joining it.So with tab1 AS (Select * from a where a.c1 < 20),tab2 AS (Select * from a where a.c2 > 2000),tab3 AS (Select * from a where a.c3 < 45),tab4 AS (Select * from a where a.c4 < 175)then select like Select * from tab1 INNER JOIN tab2 on tab1.c1 = tab2.c1INNER JOIN tab3 on tab1.c1 = tab3.c1INNER JOIN tab4 on tab1.c1 = tab4.c1-------- Code 1; Does Work--------------------Create FUNCTION Test22 ()RETURNS @tmp1 TABLE ( [Year] INT, [Month] VARCHAR(255), [Customer] VARCHAR(255) ) ASBEGINDECLARE @Begin_Date DATE , @END_Date DATE SET @Begin_Date = getdate() -7SET @End_Date = getdate() 'commenting to keep it simple'SELECT @Begin_Date = [Params1_Date] ,@END_Date = [Params2_Date] FROM [tbl_Parameters] ' WHERE Params_Name = 'Status_Rpt' INSERT @tmp1SELECT [Year] ,[Month] ,[Customer] FROM [temp_table] RETURNEND-----------------------------------------Code 2 ; Doesn't Work--------------------Create FUNCTION Test22 ()RETURNS @tmp1 TABLE ( [Year] INT, [Month] VARCHAR(255), [Customer] VARCHAR(255) ) ASBEGINDECLARE @Begin_Date DATE , @END_Date DATE SET @Begin_Date = getdate() -7SET @End_Date = getdate() 'commenting to keep it simple'SELECT @Begin_Date = [Params1_Date] ,@END_Date = [Params2_Date] FROM [tbl_Parameters] ' WHERE Params_Name = 'Status_Rpt' INSERT @tmp1With aa AS (Select * from [temp_table] where c_date between @Begin_Date AND @End_Date)SELECT [Year] ,[Month] ,[Customer] FROM AA RETURNEND---------------------------------

Viewing all articles
Browse latest Browse all 3145

Trending Articles