Solved

Sql query to print

Posted on 2016-08-17
3
41 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 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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