Suppressing query output information from the export file - sql

Trying to export a SQL Server database query results to a .csv file I have similarly output situation according to what it's presented here but having a different approach I open a new thread.
I have a complex query that has joins on few tables using some filters via some temporary tables. I am executing the stored procedure:
EXEC xp_cmdshell 'SQLCMD -S . -d myDATABASE -Q "EXECUTE myStoreProcedure;" -s " " -x -o "d:\result.csv"';
I get all the data into result.csv but the beginning of the file contains some
(10 rows affected)
(4 rows affected)
(23 rows affected)
(5 rows affected)
(8 rows affected)
(2 rows affected)
//followed by the header columns - as expected
----------------------------------------------------------------------
//and effective results - as expected
I would prefer not having output and dashed rows. I'm open to any suggestions / workarounds.

I think you want to add
SET NOCOUNT ON;
as the first line in your stored procedure
and add "-h -1" to your SQLCMD call
EDIT: #siyual beat me on the nocount part. I'll leave it in but he got it first :-)
EDIT 2: OK, I coded a short example showing what I mean and the output it produces
--EXEC xp_cmdshell 'SQLCMD -h -1 -S myserver -d Scratchpad_A -Q "EXECUTE spDummy;" -s " " -x -o "C:\TEMP result.csv"';
CREATE procedure spDummy as
SET NOCOUNT ON;
DECLARE #T TABLE( Person varchar(7), FavCol varchar(7));
INSERT INTO #T(Person, FavCol) VALUES ('Alice','Red'), ('Bob','Yellow'), ('Cindy','Green'), ('Dan','Blue');
SELECT 'Person' as H1, 'FavCol' as H2;
SELECT * FROM #T
and it gives me the output
Person FavCol
Alice Red
Bob Yellow
Cindy Green
Dan Blue
How does this compare to what you need? If that's not what you're looking for, maybe you could try a different way of explaining it?

Related

Bash script to export a table into another table

I created a script to export a table from my db into a .csv file
#!/usr/bin/bash
FILE="example.csv"
sqlplus -s abcd/abcd#XE <<EOF
SET PAGESIZE 50000
SET COLSEP ","
SET LINESIZE 200
SET FEEDBACK OFF
SPOOL $FILE
SELECT * FROM myTable;
SPOOL OFF
EXIT
and now I'd like to modify this script and export my table into another. How can I change my code?
By "exporting your table into another", do you mean copying data from one table to another? If you don't need indexes or keys or other features on your new table initially, i.e. if it's not for production use, it's quite simple:
#!/usr/bin/bash
TABLE="myOtherTable"
sqlplus -s abcd/abcd#XE <<EOF
CREATE TABLE $TABLE as SELECT * FROM myTable;
EXIT
You could also do a create table statement first, specifying columns, keys and storage options as any other table, and then have a separate line that does INSERT INTO $TABLE (SELECT * FROM myTable) to fill it with data copied from myTable.

How to get column name in excel using bcp format in sql

I already got the csv file with data but i need data with columns
I have many table which i need to execute at one go and save data in csv format at one location with header i don't want to add columns name as their 100 table.
I am using below query
Declare #cmd varchar(8000)
Set #cmd='Bcp"select * from tablename where condition queryout "path" -s -T -f -t, -c -E
EXECUTE master..xp_cmdshell
Their 100 tables like that.. any suggestion

bcp won't output temp tables

I have a stored procedure that stores values in temp tables.
It all works well, but I can not bcp it with
exec master..xp_cmdshell 'bcp "exec sp_test '2006-07-21' " queryout c:\test.txt -c '
If I change the table to regular, then it all works. Can you not use temp tables this way?
I would not necessarily want to share the code as it contains company stuff, but it is basically like this
SELECT
*
INTO #Extractr
FROM
TABLE A
WHERE ID in (4,9,14)
The error message is invalid object #Extractr
Thanks!
I have just stumbled upon this a few days ago.
What I've learned from this link:
http://www.dbforums.com/microsoft-sql-server/1605565-can-we-have-temporary-table-store-procedure-when-using-bcp.html
is that it won't see temp tables as they'd be in the tempdb database not the one you are using.
Also, I got mine working by replacing the local temp tables to global ones (## instead of # with a simple replace helped me).
As #Kevin has mentioned in the comments, you can alternatively use table variables for the same purpose.
Hope this will work for you.
Have you tried referencing the temp table like this in your query: tempdb..#Extractr
For example:
SELECT
*
INTO tempdb..#Extractr
FROM
TABLE A
WHERE ID in (4,9,14)
Using table variables instead of temp tables helped me to figure it out.
DECLARE #tbl1 TABLE
(fld1 int,...)
INSERT INTO #tbl1
SELECT * FROM Table1

How to include SQL code in SQL query?

Prior to my main select statement, I first have to create a temporary table with thousands of lines (using a select statement to get these lines automatically is not feasible in my SQL Server environment), but working in that SQL query is a nightmare of readability, as my .sql file has thousands of lines :(
Is is possible to achieve something like this ?
include('actors_tables.sql') /*including all the insert code*/
select * from #temp_actors
instead of this ?
create table #temp_actors (firstname varchar(50), lastname varchar(50))
insert into #temp_actors values ('George','Clooney')
insert into #temp_actors values ('Bill','Murray')
insert into #temp_actors values ('Bruce','Willis')
... + 1000 inserts in thousands of lines
select * from #temp_actors
Seems to me like a basic simple feature but I can't find how to achieve this ...
The server is running SQL Server 2005, and I'm using SQL Server Management Studio 2008.
Thank you for your help !
Kris.
From the command prompt, start up sqlcmd:
sqlcmd -S <server> -i C:\<your file name here>.sql -o
Or to run from sql server management studio use xp_cmdshell and sqlcmd:
EXEC xp_cmdshell 'sqlcmd -S ' + #DBServerName + ' -d ' + #DBName + ' -i ' + #FilePathName
you can use Sqlcmd in command prompt with input file (use sqlcmd -i actors_tables.sql ).

sql insert query through text file

I'm trying to insert data from excel sheet to sql database. The query is stored in a text file as follows:
insert into [demo].[dbo].[relative]
select *
from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=D:\relative.xls','select * from [sheet1$]');
When I am executing the following command:
sqlcmd -S ADMIN-PC/SEXPRESS -i d:\demo.txt.
it is showing this error:
Msg 7357, Level 16, State 2, Server ADMIN-PC\SEXPRESS, Line 1
Can anyone please help in rectifying my problem.
Try using the sql server import vizard to create a new table from the xls file and then insert that data to the existing table from there. The problem you are having is maybe due to the non-compatibility between 64bit sql instance and 32 bit excel.
Or try using bcp
bcp demo.dbo.relative in "D:\relative.xls" -c -T
There is another way to get the same result..
create a temp table.
declare #sometable table (value varchar(50), translation varchar(max))
select * into #sometable from YOUR_DATABASE_TABLE (nolock)
Then, do your OPENROWSET, BCP, etc. from here..
You can create a shell script which will automatically read the insert commands from the .csv file and then write it to the database. If you want I can help you up with it. What you just need to do is to write all the insert statements in the .csv file.
#!/bin/ksh
sqlplus -silent /nolog << EOF > /dev/null
username/pwd#"Connection String"
set linesize 0;
set pagesize 0;
set echo off;
while read line; do
A=`echo "$line" | awk -F" " {print 1}`
and so on depends on the number of words in the insert statements.
$A $B
done < your_insert_statements.csv
It will read the .csv file and automatically insert the records in the database.