sqlcmd

SQLCMD: Prompt for Variable?

时间秒杀一切 提交于 2019-12-02 05:47:18
Coming from an Oracle background, Oracle's SQLPlus would let you indicate a variable. If the variable wasn't set, you'd be prompted to provide a value. I'm using SQLCMD, using the $([var_name]) syntax. In SSMS SQLCMD mode, I get: A fatal scripting error occurred. Variable tbl_name is not defined. ...for trying to run: SELECT COUNT(*) FROM $(tbl_name) Does SQLCMD provide the same functionality as SQLPlus? If so, what am I doing wrong? Florian Reischl SQLCMD does not support prompting for missing variable names. However, you can use SSMS in SQLCMD mode. Dunno how you're error was caused, but

Using variable in sql postdeployment build script?

*爱你&永不变心* 提交于 2019-12-01 17:24:26
问题 What I would like to be able to do, is to be able to create a publising xml script in Visual Studio database project that defines a variable, that will be used to compose a script . My problem is that I get a Error: "SQL72008: Variable DeployType is not defined." if I don´t define the variable in the post deployment script like this ":setvar DeployType "varchar(100)" When I run the publish.xml the DeployType parameter is rightly set at the top of the generated script BUT that value get

Using variable in sql postdeployment build script?

拜拜、爱过 提交于 2019-12-01 17:20:10
What I would like to be able to do, is to be able to create a publising xml script in Visual Studio database project that defines a variable, that will be used to compose a script . My problem is that I get a Error: "SQL72008: Variable DeployType is not defined." if I don´t define the variable in the post deployment script like this ":setvar DeployType "varchar(100)" When I run the publish.xml the DeployType parameter is rightly set at the top of the generated script BUT that value get overridden with the ":setvar DeployType "varchar(100)". So to make this work I would need to manually remove

How to set a sqlcmd output to a batch variable?

亡梦爱人 提交于 2019-12-01 05:47:38
I'm trying to set the output of a sqlcmd query to a variable in a batch file. Here's my query: sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1" This gives me exactly what I would expect and what I want: ----------- 10 <1 rows affected> However, when I try to set it to a variable, I used this script: for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1"') do set ColumnVar=%%a echo %ColumnVar% pause This gives me this result instead: <1 rows affected> I'm guessing this is because the loop is setting the variable to the last line. So is

How to set a sqlcmd output to a batch variable?

情到浓时终转凉″ 提交于 2019-12-01 02:52:20
问题 I'm trying to set the output of a sqlcmd query to a variable in a batch file. Here's my query: sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1" This gives me exactly what I would expect and what I want: ----------- 10 <1 rows affected> However, when I try to set it to a variable, I used this script: for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1"') do set ColumnVar=%%a echo %ColumnVar% pause This gives me this result instead: <1

I need best practice in T-SQL Export data to CSV (with header)

我怕爱的太早我们不能终老 提交于 2019-11-30 21:30:03
What I need to do is export data into CSV file using T-SQL. And I'm very confused about there are many ways can do it, I don't know to choose which one, please help me to confirm the bollowing: As I know there are about 3 methods, and I want you help me to confirm: Using Microsoft.Jet.OLEDB.4.0, like this: INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Text;Database=C:\Temp\;HDR=Yes;', 'SELECT * FROM test.csv') (object_id, name) SELECT object_id, name FROM sys.tables; but this need the csv file is there, and with header using SQLCMD command line. using BCP Use union, get data and it's

Need help to write bat file that execute sql scripts in (sql server 2008 and another 3 files.?

余生长醉 提交于 2019-11-30 20:13:14
I am sure these has been asked before but cannot find clear instruction how to create a batch file lets call it "Update Database" this batch file should Execute sql scripts located in different folders Execute another 3 bat files. Any quick examples how to do it?Never done it before thanks a lot EDITED Can I do this? :On Error exit :r C:\myPath\MasterUpdateDatabase.bat GO SQLCMD -S (Local) -i C:\myPath\InsertUsername.sql I get an error: "GO" is not recognized as internal external command Thanks for any input It looks like you're trying to use DOS commands to create a batch file that either (a)

How do I call a stored procedure with arguments using sqlcmd.exe?

只谈情不闲聊 提交于 2019-11-30 18:15:19
I need to call a stored procedure and pass arguments in from Powershell. I think the best option is to use sqlcmd.exe but I'm not sure how to pass arguments to the stored proc using this tool. sqlcmd.exe supports variable substitution and parameters via the /v argument, see Using sqlcmd with Scripting Variables . For example: sqlcmd -E -d <mydb> -Q "exec usp_myproc @variable=$(myparam)" /v myparam=1 will invoke the procedure passing the value 1 to the script to be substituted for the variable $(myparam) . Note that sqlcmd variable substitution is a string replacement of $(variable) which

Return value of SQLCMD

百般思念 提交于 2019-11-30 18:09:59
I need to check the exit status (success/failure) of a query run through SQLCMD utility. For example, the server I am connecting doesn't have a database name EastWind . Then, the command below fails with the message ... > "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -S ZEPHIR -E -Q "USE WestWind" Changed database context to 'WestWind'. > echo %errorlevel% 0 > "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -S ZEPHIR -E -Q "USE EastWind" Database 'EastWind' does not exist. Make sure that the name is entered correctly > echo %errorlevel% 0 I see that the

How to use TAB as column separator in SQLCMD

一笑奈何 提交于 2019-11-30 12:39:08
SQLCMD supports the -s parameter to specify the column separator, but I couldn't figure how how to represent the tab (CHAR(9)) character. I have tried the following but both don't work: sqlcmd -S ServerName -E -Q"select * from mytable" -s"\t" -o results.txt sqlcmd -S ServerName -E -Q"select * from mytable" -s'\t' -o results.txt Any ideas how to do this in SQLCMD? In a batch file, putting a tab between the double quotes works. sqlcmd -S ServerName -E -Q"select * from mytable" -s" " -o results.txt to do the same in a PowerShell file use escaped double quotes wrapped around an escaped tab sqlcmd