Sam Myat
Sam Myat

Reputation: 1

Get the data from EXEC statement?

When I execute the query in mssql, the data is not displayed. How can I get the Data from the EXEC statement query? My Query is

DECLARE @TableName  VARCHAR
    SET     @TableName  ='Nums'
    EXEC    (
                'SELECT *
                FROM    '+"@TableName

            )

Please guide me where am i wrong?

Upvotes: 0

Views: 498

Answers (1)

Ilyes
Ilyes

Reputation: 14928

I'll do it like

DECLARE @TableName SysName = N'Nums';
DECLARE @SQL NVARCHAR(MAX) = N'SELECT * FROM ' + QUOTENAME(@TableName);

EXECUTE sp_executesql @SQL;

Finally, I would suggest that you visit sp_executesql and The Curse and Blessings of Dynamic SQL by Erland Sommarskog.

Upvotes: 2

Related Questions