Solved

Sql query to print

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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