May 27, 2013 at 1:02 am
Hi
i want to build query at runtime.
i have some metadata tables where i can store table names and attribute names. so now how can i do CRUD operations.
May 27, 2013 at 1:10 am
Create a SQL in a Variable and execute using sp_executesql procedure.
May 27, 2013 at 1:15 am
DECLARE @GblTableName NVARCHAR(300)
SET @GblTableName = '[tempdb].[dbo].[Temp' + REPLACE(CAST(NEWID() AS NVARCHAR(50)), '-', '') + ']'
EXEC ('IF OBJECT_ID(''' + @GblTableName + ''') IS NOT NULL
DROP TABLE ' + @GblTableName + '')
exec('CREATE TABLE ' + @GblTableName + '
(
UserID NVARCHAR(100) ,
ProductID NVARCHAR(100) ,
Price NVARCHAR(100) ,
ProductQty NVARCHAR(100) ,
TotalPrice NVARCHAR(100) ,
ReportPrice NVARCHAR(100)
)')
SET @ExecuteSql = N'INSERT INTO ' + @GblTableName + ' SELECT UserID ,ProductID ,Price,ProductQty,TotalPrice,ReportPrice
FROM ( SELECT USERID ,
PRODUCTID ,
PRICE ,
PRODUCTQTY ,
TOTALPRICE ,
REPORTPRICE ,
DATAORDER
FROM ABC) T '
EXEC (@ExecuteSql)
May 27, 2013 at 1:16 am
Above example is only for reference and not the perfect solution.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply