I have to debug a large stored procedure in SQL Server 2008 (also in 2005).
because I can't go in that procedure StepByStep, I need to debug it using some output files.
actually i use something like
DECLARE #SQL VARCHAR(8000)
SELECT #SQL = 'BCP "SELECT * FROM MY_TABLE" QUERYOUT "D:\TDB\test.txt" -C -T -w'
EXEC MASTER..XP_CMDSHELL #SQL
but this approach has multiple limitations like impossibility to use # tables, and a complex way to use filters (where X='+cast(#MYLocalVar as varchar)+')...
Is there a other way to output a select to a file, like MySql does
SELECT * into outfile '../../htdocs/VIP/Temp/temp.txt' from tmp_Menu2;
You can put the data into a table quite readily:
SELECT *
into anotherdatabase..outtable
from . . .
This creates a new table with whatever columns you want.
To output something into a file requires an extra step, using bulk export.
Related
How to populate a .csv file from a SQL Server stored procedure?
We don't have Office on the Server. The .CSV file has to be populated from a stored procedure result set.
How to export to a .CSV without using SSIS package?
End result, I will have to generate email alert by attaching this CSV file as report.
I will have to use bulk copy program utility (BCP), I am looking on samples for BCP to generate csv file
There are two methods to achieve that:
(1) Using OPENROWSET
Try implementing a similar logic:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
EXEC Sp_TEST
(2) Using bcp
From the third link in References section:
The queryout method allows you to BCP from the result of a stored procedure, which opens up a lot of possibilities and offers a lot of control over the file format. For anything other than a simple table extract I would tend to use this method rather than a view. I would also format each line within the stored procedure. This means that the formatting can be tested independently from the file creation
declare #sql varchar(8000)
select #sql = 'bcp "exec sp_Test"
queryout c:\bcp\sysobjects.csv -c -t, -T -S'
+ ##servername
exec master..xp_cmdshell #sql
References
How To Export Data To the .csv file using Sql server Stored Procedure.
How to produce an csv output file from stored procedure in SQL Server
Creating CSV Files Using BCP and Stored Procedures
Exporting a csv file via stored procedure
Writing select result to a csv file
Look at the code below. A stored procedure is created and then used as the source in the BCP command. Then a text file is generated. Delimiters are discussed in the code sample in the link.
Code sample taken from Creating CSV Files Using BCP and Stored Procedures
use tempdb
go
create proc s_bcpMasterSysobjects
as
select '"' + name + '"'
+ ',' + '"' + convert(varchar(8), crdate, 112) + '"'
+ ',' + '"' + convert(varchar(8), crdate, 108) + '"'
from master..sysobjects
order by crdate desc
go
declare #sql varchar(8000)
select #sql = 'bcp "exec tempdb..s_bcpMasterSysobjects"
queryout c:\bcp\sysobjects.txt -c -t, -T -S'
+ ##servername
exec master..xp_cmdshell #sql
I would use BCP
see example
declare #sql varchar(2000)
select #sql = 'bcp "SQL QUERY HERE" queryout '+#fileLocation+#fileName+'.csv -c -t, -T -S' + ##servername
exec master..xp_cmdshell #sql, NO_OUTPUT
if you need to see any errors or whats it's doing, just remove the ", NO_OUTPUT"
Create a little script, the csv is a simple format : csv = comma separate value. Separate your values width comma.
I am using the below script for exporting data from table, which is working completely fine.
declare #sql varchar(8000)
select #sql = 'bcp "Select * from [CV18].dbo.ZMM002" queryout D:\Share\Vendor_portal_Pending_IBD\bcptest.txt -c -t^| -T -S'+ ##servername
exec master..xp_cmdshell #sql
But I want to add headers to it. I have found out a way to do that by using UNION ALL with header names.
select 'Counter','External_ID','Delivery_Date','Transport_ID','Bill_of_Lading','Delivery_Item','Material','Delivery_QTY',
'Unit','PO_Number','PO_Item'
union all
select * from ZMM002
But how can I add this union all query in my bcp query?
I get a syntax error, because I am using single quotes (') in union all query, which is cutting off the outer single quote, i.e #sql = ' '
In my opinion it is better to do this as a post-op.
Export two files, one with the header, the other with the actual data. Afterwards, concatenate the two files using the COPY command.
Suppose you exported to header.txt and data.txt, the command would be
COPY /b "\\path\header.txt"+"\\path\data.txt" "\\path\data.dat"
Where \\path would be your actual path.
If you're formatting the header SQL, double the qoutes:
SET #sql='select ''Counter'',''External_ID'',''Delivery_Date'',''Transport_ID'',''Bill_of_Lading'',''Delivery_Item'',''Material'',''Delivery_QTY'',''Unit'',''PO_Number'',''PO_Item''';
I have a sql query to get the count of a table
select count(*) from table_name_ID;
the above query is working as expected but the ID in table name has to be fetched from another parameter table and execute it in shell script and output has to be mailed to the user
tried multiple ways and failed, could someone help me out here?
Try like this:
DECLARE #Query VARCHAR(5000)
SET #Query ='BCP.EXE "SELECT * FROM '+DB_NAME()+'..Table_Name" queryout "D:\TEST\Test.txt" -T -c'
EXEC master..xp_cmdshell #Query
I'm using a software that generates and feeds data continuously. This data is shown in my own software and can be extracted using report generation (option available).
The limit of data storage is 500 rows, so after every 500 rows of data, I have to extract using report and then open SQL Server and use truncate (table_name) and execute it to delete it.
I want a SQL command which extracts data after every 500 rows and saves that data in a defined location and then clears (truncate) the base table.
In SQL Server you can do this in a stored proc using IF... ELSE construct.
Here is the MSDN Documentation that explains how to use the construct...
The counting part will look like this-
IF ((SELECT COUNT(PC.id) FROM [server08].[db01].[dbo].[tblesrc01] PC) > 500)
BEGIN
TRUNC ...
INSERT ...
END
And for the saving data part : you can use bcp command. It allows you to export the result set from a Transact-SQL statement to a data file. The Transact-SQL statement can be any valid statement that returns a results set, such as a distributed query or a SELECT statement joining several tables.
The following example exports the names from the AdventureWorks2008R2 Person.Person table into the Contacts.txt data file. The names are ordered by last name then first name.
The example is executed from the Microsoft Windows command prompt:
bcp "SELECT FirstName, LastName FROM AdventureWorks2008R2.Person.Person ORDER BY LastName, Firstname" queryout Contacts.txt -c -T
Executing bcp inside a stored procedure--
DECLARE #job NVARCHAR(100)
SET #job ='execute_bcp'
EXEC msdb..sp_add_job #job_name = #job,
#description = 'Execute bcp command',
#owner_login_name = 'sa',
#delete_level = 1
EXEC msdb..sp_add_jobstep #job_name = #job,
#step_id = 1,
#step_name ='Command Shell Execution', #subsystem = 'CMDEXEC',
#command = 'bcp "SELECT Name FROM [DatabaseName].[dbo].[Employees]" queryout "filepath.txt" -c -T',
#on_success_action =1
EXEC msdb..sp_add_jobserver #job_name =#job
EXEC msdb..sp_start_job #job_name = #job
I have a certain script which I need to execute across all databases in SQL Server. I have used the following logic to loop through all database excluding the system databases.
declare #DBName varchar(500)
DECLARE #Database_id int
DECLARE #Query varchar(MAX)
select #Database_id = MIN(database_id) from sys.databases where database_id>4
while #Database_id is not null
begin
select #DBName=name from sys.databases where database_id=#Database_id
set #Query = 'Use'+#DBName
--some script
Print ''+#DBName+''
exec (#Query)
select #Database_id = MIN(database_id) from sys.databases where database_id>4 AND database_id>#Database_id
end
But the problem is I can't use the 'Use' command with a variable. Is there any other way to make use of 'Use' command to hit the database?
And I don't want to use sp_MSforeachdb command as my query is too long.
Any help would be appreciated!
There is an undocumented function doing this. You can use the question mark in the place of the db's name:
EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_spaceused'
Find this example and more explanations here: http://weblogs.sqlteam.com/joew/archive/2008/08/27/60700.aspx
EDIT: From your comments above I find, that you do not want to includ the system dbs into your run. It seems to be q bit tricky to detect, whether a db is a system db or not but you could go like this:
DECLARE #cmd VARCHAR(1000)='USE [?];IF DB_ID(DB_NAME())<=4 RETURN;EXEC sp_spaceused;'
EXECUTE master.sys.sp_MSforeachdb #cmd;
GO
Read about the flaws here (about SQL Server 2005!): SQL Server: How to tell if a database is a system database?
If you don't like this <=4 you could simply do it like
IF DB_NAME() IN('master','model',... any more names here ... ) RETURN;
The only real restriction I know of is, that sp_MSforeachdb does not like "GO".