Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Table-Valued Parameters and JOINS

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2013-10-23 : 13:44:12
I have a user defined table:

CREATE TYPE [dbo].[int_list_tbltype] AS TABLE(
[ID] INT NULL
)
GO


I am testing some options but just can't seem to get the sql correct:

DECLARE @myTVP dbo.int_list_tbltype

INSERT INTO @myTVP ([ID])
VALUES (3)

SELECT v.Status, v.Item
FROM myView v
INNER JOIN @myTVP tvp ON v.SocStatus = tvp.ID



If the tvp has values, the INNER join works fine.
But at times there my be no values at all, so I want to bring back all the rows from myView.
Right and Left joins don't seem to do the trick.

Any suggestions?

Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-23 : 13:51:15
[code]
DECLARE @myTVP dbo.int_list_tbltype

INSERT INTO @myTVP ([ID])
VALUES (3)

SELECT v.Status, v.Item
FROM myView v
LEFT JOIN @myTVP tvp ON v.SocStatus = tvp.ID
WHERE tvp.ID IS NOT NULL
OR (SELECT COUNT(*) FROM @myTVP) = 0
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2013-10-23 : 13:56:16
Worked perfectly!

Very nice. Thank You!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-23 : 14:06:45
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -