Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Trap output generated to a variable
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

skc40
Starting Member

34 Posts

Posted - 11/18/2013 :  16:26:16  Show Profile  Reply with Quote
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

USA
38200 Posts

Posted - 11/18/2013 :  16:35:26  Show Profile  Visit tkizer's Homepage  Reply with Quote
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 - 11/18/2013 :  16:40:00  Show Profile  Reply with Quote
TY, definitely helps
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000