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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-06-28 : 07:42:06
|
| Wilfred writes "I am new to sql:Basically i want to select a value into a variable but the table can change.basically i want to know a way of accomplishing a query like thisselect sqlstr='SELECT @ObjTbId = ID FROM '+@sysobj+' WHERE ID = object_id('+@objtbName+')'print @ObjTbId where by @sysobj table can changeI have appened the whole function bellow if u need itSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GO/** * Object Name: fn_getTableIdByName * Object Type: Pcikfords User Defined Function * Programmer : wokoli * ALTER Date: 18 -June -2004 * Description: Return id for a table object * Revision History: * * Parameter @objtbName - table name */ALTER function fn_getTableIdByName( @objtbName varchar(1000))RETURNS intASBEGIN DECLARE @ObjTbId INT DECLARE @sqlstr nvarchar(1000) DECLARE @sysobj varchar(1000) select @ObjTbId=-1 SELECT @sysobj=PARSENAME('pubs.dbo.syscolumns',3)+'.'+ PARSENAME('pubs.dbo.syscolumns',2)+'.syscolumns'select sqlstr='SELECT @ObjTbId = ID FROM '+@sysobj+' WHERE ID = object_id('+@objtbName+')' exec (@sqlstr) IF @ObjTbId IS null SELECT @ObjTbId = xusertype FROM systypes WHERE NAME = @objtbName RETURN @ObjTbIdENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOThan you verry much in advance" |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-06-28 : 08:28:52
|
| Seehttp://www.nigelrivett.net/sp_executeSQL.htmldeclare @i intexec sp_executesql N'select @i = 999', N'@i int output', @i outputselect @i==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|