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 2008 Forums
 Transact-SQL (2008)
 Trap output generated to a variable

Author  Topic 

skc40
Starting Member

34 Posts

Posted - 2013-11-18 : 16:26:16
Hi All,
The @FieldName_Varchar variable is dynamic, but for testing purpose declare variable as below.

How can I generate and trap value generated from @SQL statement below to @Output_Data without using temp table/table variable?

EMPLOYEEID varchar(100) is 20099 for Emp_ID below.

I want to generate @Output_Data=20099!!

DECLARE @FIELDNAME VARCHAR(200),
@EMP_ID INT,
@SQL NVARCHAR(500),@Output_Data VARCHAR(100)

SET @EMP_ID=100

SET @FIELDNAME='EMPLOYEE_INFO.EMPLOYEEID'

SET @SQL='SELECT '
SET @SQL= @SQL + @FieldName
SET @SQL= @SQL + ' FROM EMPLOYEE_INFO WHERE EMPLOYEE_INFO.EMPLOYEEID=' + CAST(@EMP_ID AS VARCHAR)

PRINT @SQL
--EXECUTE sp_executesql @SQL

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-11-18 : 16:35:26
See example C for the OUTPUT option of sp_executesql: http://technet.microsoft.com/en-us/library/ms188001.aspx

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

skc40
Starting Member

34 Posts

Posted - 2013-11-18 : 16:40:00
TY, definitely helps
Go to Top of Page
   

- Advertisement -