Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 dynamically creating an output file name in sqlcmd

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2010-03-18 : 13:20:36
Not sure whether this is the right place for such a question, but i have a work colleague in need .......

Is it possible to output the results of a table from a DB into a filename that is dynamically generated using variables obtained from another query?

For example, I have a table called Orders
I want to extract the data for a specific day, and output to a filename called Orders_20100318
The date part of the file name will be extracted from a table using a query

Anyone??

Hearty head pats

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-18 : 13:36:13
thats possible but you need to use dynamic sql for that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2010-03-18 : 13:39:58
Hi there

Thanks for the response.

So in the simple example below, I have 2 tables, one with the data to be extracted, and the other with the date to append to the filename:

USE [master]
GO
DROP DATABASE HelpAl
GO
CREATE DATABASE HelpAl
GO
USE HelpAl
GO
CREATE TABLE SimpleDate (AlsDate DATETIME)
CREATE TABLE SomeData (AlsData VARCHAR(10))

INSERT INTO SimpleDate (AlsDate)
SELECT CURRENT_TIMESTAMP

INSERT INTO SomeData (AlsData)
SELECT 'HELLO'
GO

This is the sqlcmd (I saved to a bat file to execute):

sqlcmd -S WS23\SQLSERVER2008 -E -d HelpAl -Q "SELECT * FROM SomeData;" -o D:\Results.bat -h -1


How do I use dynamic sql to create the filename Results_20100318.bat?

Thanks

Hearty head pats
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-18 : 13:43:42
see this as an example

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/bcp-export-data-to-text-file.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2010-03-18 : 13:56:02
Thanks Visakh16

I've now got the following:

DECLARE @str NVARCHAR(1000), @Date CHAR(8)

SELECT @Date = CONVERT(VARCHAR,AlsDate,112) FROM SimpleDate

SET @str='sqlcmd -S WS23\SQLSERVER2008 -E -d HelpAl -Q "SELECT * FROM SomeData;" -o D:\Results_'+@Date+'.bat -h -1'

SELECT @str
EXEC (@str)


When I try to execute in Management Studio (with sqlcmd mode enabled, I get the error:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'S'.

I was getting this previously, hence used a bat file to execute the sqlcmd. Is there somethign I am missing, as I thought you could use the utility in MS if the mode was enabled?

Hearty head pats
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-18 : 14:15:45
You have to call sqlcmd.exe from xp_cmdshell inside T-SQL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2010-03-19 : 04:44:46
Ah, thank you!

Hearty head pats
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-19 : 15:52:44
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -