Field Name stored in variable

Is it possible to do this?

DECLARE @TableName As varchar(50)
set @TableName = 'tbl_goccurrence'   
DECLARE @OccurrenceFileNo As varchar(50)
    exec ('select ' + @OccurrenceFileNo + ' = occurrencefileno from @TableName where id=902000000001387589')
    print @OccurrenceFileNo

The error I get is: Incorrect syntax near '='

Answers


You can use sp_executesql with an output parameter to assign the value to @occurrenceFileNo.

DECLARE @OccurrenceFileNo As varchar(50)
exec sp_executesql N'select @OccurrenceFileNo = occurrencefileno from tbl_goccurrence where id=902000000001387589', N'@OccurrenceFileNo varchar(50) out', @occurrenceFileNo out
print @OccurrenceFileNo

Need Your Help

iText java ee, add image header from upload temp, help to resolve

html pdf header itext

I have this method, header is inserted but image is missing in header. :-( Every tutorial is based on C:\\imagelocation\\dsadsa.jpg

POST array is empty when containing a file

php forms post file-upload

I have a simple HTML form containing a file input. When the form is submitted without a file, printing the $_POST array shows me all of the data submitted. When a file is submitted, however, $_POST

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.