Solved

Sql query to print

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FrontEnd tools to create web database application 7 82
date diff with Fiscal Calendar 4 55
Sql Server group by 10 44
sql 2016 data tools breakdown.. 1 15
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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