SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to create dynmaic virtual table using meata data tables


How to create dynmaic virtual table using meata data tables

Author
Message
sravanthi033
sravanthi033
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 10
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.
Bhaskar.Shetty
Bhaskar.Shetty
SSC Eights!
SSC Eights! (862 reputation)SSC Eights! (862 reputation)SSC Eights! (862 reputation)SSC Eights! (862 reputation)SSC Eights! (862 reputation)SSC Eights! (862 reputation)SSC Eights! (862 reputation)SSC Eights! (862 reputation)

Group: General Forum Members
Points: 862 Visits: 509
Create a SQL in a Variable and execute using sp_executesql procedure.
Aadhar Joshi
Aadhar Joshi
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1674 Visits: 451
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)
Aadhar Joshi
Aadhar Joshi
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1674 Visits: 451
Above example is only for reference and not the perfect solution.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search