Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql query to print

Posted on 2016-08-17
3
Medium Priority
?
46 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

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 41

Accepted Solution

by:
Sharath earned 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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