Sometimes, we need to copy the data from SQL Server result set to any external source (excel, word, notepad etc.) for different purposes. But the problem is when you usually copy (Ctrl+ C or right click >> Copy) and paste it into external source, the column name is not copied and you need to type it manually.
As you can see in the given below images, we copied data from SQL Server and pasted it in the excel but column name did not appear in the excel.
SOLUTION 1 :
The solution is a permanent one, meaning it will copy the column header from the query result set everytime. But, I usually do not recommend this solution because sometimes you do not need the column header and in this case you need to manually delete the column header. Opsssssssssssss.
So what is the solution ? Basically it is a built-in feature in SQL Server.
Let me explain it step by step.
Step 1 :
You need to browse Tools menu and select Options as shown in the image below.
Step 2 :
Once you select Options menu, it will open options dialogue box.
Now you need to select Query results\ SQL Server\Results to Grid from the left hand pane and place the check mark on Include column headers when copying or saving the results in the right hand pane and press OK. Given below is the screen image.
Step 3 :
Now, run query in any window, select the result set (data) then copy (Ctrl+C) and paste it into any external application. This time, it will copy the column header as well.
SOLUTION 2 :
In this solution, you need not go to multiple screens and set any options. In a way, it’s a shortcut to the above solution. The most important benefit using this solution is, if you need the header you can copy it, else copy the data only.
Step 1 :
Run query in any window, select the result set (data) and right click on it as shown in the image below.
Step 2 :
Once you right click on the selected result set, you can either copy without header (Ctrl+C) or copy with header (Ctrl+Shift+C) and paste it into any external application.
Hi Imran,
Say I have a SP, named QSG.dbo.EXCEL_FUNC, and I need to create a SQL Server Management Studio code that would generate xy table. The table x side (rows) has two variables @co_type and @se_term. The table y side has one variable @metric_type. Each time EXEC runs, it uses different combination of xy.
For example, below the 3 variables are defined in one EXEC run as: ‘NOMINALCO’, ‘2Year’, ‘Duration’
EXEC QSG.dbo.EXCEL_FUNC
@asofdate = ’11/30/2012′,
@startdate = ’10/31/2012′,
@co_type = ‘NOMINALCO’,
@se_term = ‘2Year’,
@metric_type = ‘Duration’
To generate the output table, names of those 3 variables for each EXEC run have to be defined and be passed on the fly to each EXEC run, in a loop I guess.
Any smart way to do this? Thanks in advance…
Mike
Hi Mike,
Given below is the solution.
https://raresql.com/2013/07/25/sql-server-how-to-pass-parameters-to-an-sp-using-select-command-without-variables/
Imran
Imran,
My table is 2 ID Row, 1 ID Column table, and the 3 IDs are variables: @co_type, @se_term, @metric_type. How “INSER INTO” or “While” loop can be constructed in SQL?
Thanks,
Mike