Solved

Sql query to print

Posted on 2016-08-17
3
23 Views
Last Modified: 2016-08-24
Hello,
How can I make a parameterised query to save in another table.
Example
I have a stored procedure
Select * from table where column1  = @column1

Insert into table2 values ( the above select query)

Cheers
0
Comment
Question by:RIAS
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 41760304
If both table1 and table2 have same column names, you can try like this.
Insert into table2 Select * from table1 where column1  = @column1

Open in new window

If not, explicitly mention the column names.
Insert into table2(col1,col2) Select col1,col2 from table1 where column1  = @column1

Open in new window

0
 

Author Comment

by:RIAS
ID: 41761362
Hello Sharath,

Thanks for your suggestion.But, I need to copy the query in table2 and not execute it .

table2 has column sqlquery in that the query executed has to be copied same as what the print does

Cheers
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41761473
If I understand correctly, in table2, you have a column called sqlquery which has a query. you want to execute that and load the data into table1.  If my assumption is correct, you can do something like this.
declare @column1 int -- change this datatype as per your column datatype
declare @sqlquery nvarchar(max)
select @column1 = 10 -- change this to your need
select @sqlquery = sqlquery from table2 where column1 =@column1
select @sqlquery = 'insert into table1 ' + @sqlquery
exec(@sqlquery)

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now