Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Retrieving a value
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00728752
Message ID:
00728844
Views:
17
Try
DECLARE @COLNUM smallint
DECLARE @Sql nvarchar(1000)

DECLARE @TESTFIELD varchar(100)
DECLARE @RESULT varchar(100)

SET @COLNUM = 3
USE Northwind
set @TESTFIELD = COL_NAME(OBJECT_ID('dbo.Employees'),@COLNUM )

SET @Sql = N'SELECT TOP 1 @result=' + @TESTFIELD + ' FROM Northwind.dbo.Employees'

EXEC sp_executesql @Sql, N'@result nvarchar(100) OUTPUT', @result OUTPUT
PRINT @result
See How to return a value from dynamicaly executed T-SQL code FAQ #8130 for more details.

>I need to pass a column number to an SP and get the value of that column from a cursor. Currently I have the following;
>
>
>CREATE PROCEDURE AAA
>	@COLNUM smallint
>AS
>
>DECLARE @TESTFIELD varchar(100)
>DECLARE @RESULT varchar(100)
>
>set @TESTFIELD = COL_NAME(OBJECT_ID('Northwind.dbo.Employees'),@COLNUM )
>
>DECLARE Employee_Cursor CURSOR FOR
>SELECT @TESTFIELD FROM Northwind.dbo.Employees
>
>OPEN Employee_Cursor
>
>FETCH NEXT FROM Employee_Cursor into @RESULT
>
>PRINT @RESULT
>
>CLOSE Employee_Cursor
>DEALLOCATE Employee_Cursor
>GO
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform