SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


create table valued function


create table valued function

Author
Message
SQL_Surfer
SQL_Surfer
Say Hey Kid
Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)

Group: General Forum Members
Points: 663 Visits: 1086
i need to create a function passing those two varaibales. How do i do that? Table valued function

select DISTINCT bb.level , 'Myvalue5' as'Table'
From BaseTable bb
Left Join dbo.a on (bb.col1 = a.col1)

UNION ALL

select DISTINCT bb.level , 'Myvalue4' as'Table'
From BaseTable bb
Left Join dbo.a on (bb.col1 = a.col1)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16888 Visits: 19122
You don't have any variables in your code.
You can check the CREATE FUNCTION syntax in here http://msdn.microsoft.com/es-es/library/ms186755%28v=sql.90%29.aspx

CREATE FUNCTION dbo.Example_function_name 
(
@Parameter1 int
)
RETURNS TABLE
AS
RETURN
SELECT bb.level , 'Myvalue5' as'Table'
From BaseTable bb
Left Join dbo.a on (bb.col1 = a.col1)
UNION
SELECT bb.level , 'Myvalue4' as'Table'
From BaseTable bb
Left Join dbo.a on (bb.col1 = a.col1) ;




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
SQL_Surfer
SQL_Surfer
Say Hey Kid
Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)

Group: General Forum Members
Points: 663 Visits: 1086
I wanted to make level andy Myvalue as a paramter. Can that be done?
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26520 Visits: 17557
SQL_Surfer (7/26/2013)
I wanted to make level andy Myvalue as a paramter. Can that be done?


Not exactly sure what "level andy Myvalue" is but you can add parameters to your function, just put in a comma and another parameter.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SQL_Surfer
SQL_Surfer
Say Hey Kid
Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)

Group: General Forum Members
Points: 663 Visits: 1086
level is column but myvalue is just a label (but changes) on each union.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26520 Visits: 17557
Total shot in the dark because there is nothing to work and the description is very vague but maybe you want something like this?


CREATE FUNCTION dbo.Example_function_name
(
@Level int,
@MyValue varchar(10)
)
RETURNS TABLE
AS
RETURN
select DISTINCT bb.level , SomeOtherColumn as 'Table'
From BaseTable bb
Left Join dbo.a on (bb.col1 = a.col1)
where bb.level = @Level
and SomeOtherColumn = @MyValue

UNION ALL

select DISTINCT bb.level , SomeOtherColumn as'Table'
From BaseTable bb
Left Join dbo.a on (bb.col1 = a.col1)
where bb.level = @Level
and SomeOtherColumn = @MyValue



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SQL_Surfer
SQL_Surfer
Say Hey Kid
Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)

Group: General Forum Members
Points: 663 Visits: 1086
How can i write a recursive function for this?


select col1, col2, col3
From BaseTable
Left Join dbo.Table6 Table6 on (BaseTable.z = Table6.ID6)
Left Join dbo.Table5 Table5 on (BaseTable.z = Table5.ID5)
Left Join dbo.Table4 Table4 on (Table5.ID4 = Table4.ID4)
Left Join dbo.Table3 Table3 on (Table4.ID3 = Table3.ID3)
Left Join dbo.Table2 Table2 on (Table3.ID2 = Table2.ID2)
Left Join dbo.TableZ TableZ on (Table2.ID1 = TableZ.ID1)
where ID6 IS NOT NULL AND ISNULL(TableZ.MyCol,'') + '/' + ISNULL(Table2.MyCol,'') + '/' + ISNULL(Table3.MyCol,'')
+ '/' + ISNULL(Table4.MyCol,'')+ '/' + ISNULL(Table5.MyCol,'') IS NOT NULL + '/' + ISNULL(Table6.MyCol,'') IS NOT NULL

UNION ALL


select col1, col2, col3
From BaseTable
Left Join dbo.Table5 Table5 on (BaseTable.z = Table5.ID5)
Left Join dbo.Table4 Table4 on (Table5.ID4 = Table4.ID4)
Left Join dbo.Table3 Table3 on (Table4.ID3 = Table3.ID3)
Left Join dbo.Table2 Table2 on (Table3.ID2 = Table2.ID2)
Left Join dbo.TableZ TableZ on (Table2.ID1 = TableZ.ID1)
where ID5 IS NOT NULL AND ISNULL(TableZ.MyCol,'') + '/' + ISNULL(Table2.MyCol,'') + '/' + ISNULL(Table3.MyCol,'')
+ '/' + ISNULL(Table4.MyCol,'')+ '/' + ISNULL(Table5.MyCol,'') IS NOT NULL

UNION ALL

select col1, col2, col3
From BaseTable
Left Join dbo.Table4 Table4 on (BaseTable.z = Table4.ID4)
Left Join dbo.Table3 Table3 on (Table4.ID3 = Table3.ID3)
Left Join dbo.Table2 Table2 on (Table3.ID2 = Table2.ID2)
Left Join dbo.TableZ TableZ on (Table2.ID1 = TableZ.ID1)
where Table4.ID4 IS NOT NULL AND ISNULL(TableZ.MyCol,'') + '/' + ISNULL(Table2.MyCol,'') + '/' + ISNULL(Table3.MyCol,'') IS NOT NULL

UNION ALL

select col1, col2, col3
From BaseTable
Left Join dbo.Table3 Table3 on (BaseTable.z = Table3.ID3)
Left Join dbo.Table2 Table2 on (Table3.ID2 = Table2.ID2)
Left Join dbo.TableZ TableZ on (Table2.ID1 = TableZ.ID1)
where Table3.ID3 IS NOT NULL AND ISNULL(TableZ.MyCol,'') + '/' + ISNULL(Table2.MyCol,'') + '/' + ISNULL(Table3.MyCol,'') IS NOT NULL

UNION ALL
select col1, col2, col3
From BaseTable
Left Join dbo.Table2 Table2 on (BaseTable.z = Table2.ID2)
Left Join dbo.TableZ TableZ on (Table2.ID1 = TableZ.ID1)
where Table2.ID2 IS NOT NULL AND ISNULL(TableZ.MyCol,'') + '/' + ISNULL(Table2.MyCol,'') IS NOT NULL

UNION ALL

select col1, col2, col3
From BaseTable
Left Join dbo.TableZ TableZ on (BaseTable.z = TableZ.ID1)
where TableZ.ID1 IS NOT NULL AND ISNULL(TableZ.MyCol,'') + '/' IS NOT NULL
SQL_Surfer
SQL_Surfer
Say Hey Kid
Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)

Group: General Forum Members
Points: 663 Visits: 1086
Is recursive CTE possible to rewrite this? If yes, how?
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2076 Visits: 10385
It's not really possible to tell if a rCTE would return your resultset more efficiently. Can you post the actual query rather than pseudocode? There is a far more efficient way of writing this query with your results appearing columnwise instead of rowwise, but it would depend upon the SELECT list.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
SQL_Surfer
SQL_Surfer
Say Hey Kid
Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)Say Hey Kid (663 reputation)

Group: General Forum Members
Points: 663 Visits: 1086
Can you give me some pointers of rewriting this query? Can data be grabbed at 1 shot instead of doing UNION ALL.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search