Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Any way to rerwirte this query?


Any way to rerwirte this query?

Author
Message
SQL_Surfer
SQL_Surfer
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 1076
My udf needs to be the first statement in the query and then there is a left join to a table. How can I rewrite this?

I know if table is first statement and udf is a second statement in the join, we can simply pass the col from first table to the udf

But I am confused on this one.

Please help

select
From dbo.udf_Myfunction(a.col1) udf
LEFT JOIN mytable a on udf.col = a.col1
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8564 Visits: 18143
It makes no sense that your udf needs to come before your table if it uses a column from your table.
This might help you:

SELECT *
FROM mytable a
OUTER APPLY dbo.udf_Myfunction(a.col1) udf




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
Sean Lange
Sean Lange
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: 16632 Visits: 17024
I am guessing that what you really want is all the rows from the function and values from the table if there is a match?

This is where you can turn your join around to a right join.


select *
From mytable a
RIGHT JOIN dbo.udf_Myfunction(a.col1) udf on udf.col = a.col1



_______________________________________________________________

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 Moden's 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)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8564 Visits: 18143
Sean Lange (1/7/2014)
I am guessing that what you really want is all the rows from the function and values from the table if there is a match?

This is where you can turn your join around to a right join.


select *
From mytable a
RIGHT JOIN dbo.udf_Myfunction(a.col1) udf on udf.col = a.col1


Hey Sean,
The parameter from the udf is a column from the table (I guess you didn't notice), so he would need to use APPLY. Even if a JOIN would be possible, RIGHT JOIN would make no sense as it shouldn't return any values not present on the table.


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
Sean Lange
Sean Lange
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: 16632 Visits: 17024
Luis Cazares (1/7/2014)
Sean Lange (1/7/2014)
I am guessing that what you really want is all the rows from the function and values from the table if there is a match?

This is where you can turn your join around to a right join.


select *
From mytable a
RIGHT JOIN dbo.udf_Myfunction(a.col1) udf on udf.col = a.col1


Hey Sean,
The parameter from the udf is a column from the table (I guess you didn't notice), so he would need to use APPLY. Even if a JOIN would be possible, RIGHT JOIN would make no sense as it shouldn't return any values not present on the table.


/facepalm

I didn't notice the parameter was from the table.

I figured he wanted the values from the function and nulls when there was no match.

/me slithers away quietly hoping nobody will notice how awful his original suggestion was. :-P

_______________________________________________________________

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 Moden's 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)
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