|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
passing a table name through a parameter
Hi there..
I created the following FUNCTION and I am trying to pass as a parameter a variable name withe the TABLE name. MS SQL is not accepting it because it is asking me to declare it... can someone help me?? Thankyou "CREATE FUNCTION getNSR2 (@tablename varchar(30)) RETURNS decimal(9,0) AS BEGIN DECLARE @TB varchar(30) SET @TB = @tablename DECLARE @SR decimal(9,0) SET @SR= ( SELECT MAX(SysRef)+1 FROM @tablename ) RETURN(@SR) END " |
|
#2
|
|||
|
|||
|
Your select statement will not work like that. Instead, build the statement into a string and then use exec.
e.g. ------------ DECLARE @TableName VarChar(100), @Query VarChar(500) SET @TableName = 'CheeseDoughnuts' SET @Query = 'SELECT * FROM '+@TableName+' (NOLOCK) ORDER BY Sweetness' EXEC (@Query) |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > passing a table name through a parameter |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|