Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference Columns in Table Parameter 1

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I have this as a parameter in a stored procedure.
Code:
@Tvp Tvp_UserMasterSearch READONLY
Is there a way to reference columns in @Tvp like this?
Code:
If @Tvp.UserID IS NOT NULL
I've read that you have to copy to a temp table, but I still couldn't reference any columns. I want to pass in some search values without making each one a parameter in the proc. Any help would be appreciated.

Auguy
Sylvania/Toledo Ohio
 
Well, what you do with tables is query from them, and in the query you can give a table an alias, this also works for table valued parameters, so what you can do is:

Code:
SELECT * FROM @TVP as users WHERE users.userid IS NOT NULL

There's no such concept as accessing a column standalon in an IF. @Tvp is a table, not a record, even if the table has one record only.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Thanks Olaf. The table I'm passing has only one record with a few columns like UserID, UserType, etc. Is there a way to get to that first row and then reference the column values?

Auguy
Sylvania/Toledo Ohio
 
If you know that you can define, for example:

Code:
DECLARE @userid as int
SELECT @userid = userid FROM @TVP

With multiple records you get the last rows userid stored into the variable, but with just one row in @tvp this will do, even without aliasing it. But @tvp never is just a reference to rows, it always is a reference to the whole table. You don't have the concept of SQL queries table.column outside of queries.

Bye, Olaf.



Olaf Doschke Software Engineering
 
Thanks Olaf, you are always helpful!

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top