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 2000 Forums
 Transact-SQL (2000)
 Dynamic Select

Author  Topic 

putsik
Starting Member

2 Posts

Posted - 2004-06-08 : 04:47:38
Hi,

Is it possbile to create a dynamic script in SQL?
Example,

Declare @CustomerID nvarchar(2),
@CustomerName nvarchar(12),
@DynamicFieldName nvarchar(100)

DECLARE Customer_cursor Cursor
FOR SELECT cUSTOMERiD,CUSTOMERnAME From Customer

open Customer_Cursor
FETCH NEXT FROM Customer_Cursor
INTO @CustomerID,@CustomerName

WHILE @@FETCH_STATUS = 0
BEGIN
SET @DynamicFieldName = '@' + 'CustomerID'
PRINT @DynamicFieldName
--I want to output not @CustomerID but the actual @CustomerID value
FETCH NEXT FROM Customer_Cursor
INTO @CustomerID,@CustomerName
END
close Customer_Cursor
deallocate customer_Cursor

Ty


AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2004-06-10 : 19:18:35
Nope, can't do it like this. @CustomerID is a variable defined in your procedure, you cannot dynamically re-build that variable name and reference it.

But surely this must be an oversimplification of what you're really trying to do, because otherwise you would just do PRINT @CustomerID, right?

-----------------------------------------------------
Words of Wisdom from AjarnMark, owner of Infoneering
Go to Top of Page
   

- Advertisement -