SQLCMD: Prompt for Variable? - sql

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?

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 this works for me:
:setvar table "sys.tables"
SELECT * FROM $(table);

There is a free tool "SQLS*Plus" which is an SQL*Plus for SQL Server. Works with SQL Server 2000/2005 and 2008
Very flexible with data formatting (set lines size, pagesize, etc), variables (&, &&), spool, etc - light years better than isql, osql or sqlcmd
I downloaded SQLS*Plus from http://www.memfix.com

Related

Generated script of schema and table data not running in MS SERVER sqlcmd

I have a very large table script that is about ^ GB in size and cannot open in in Query Editor (obviously) due to memory/size.
I am trying to run it on the db server with the command propmt and using sqlcmd:
I am 100% sure the path and script name are correct (marked out for privacy reasons). I then used the following two scripts to get the DBServer\SQLInstance:
SELECT ##servername
SELECT ##servicename
What am I missing as it appears it has not done anything with the 21? _ prompt just sitting there. Do I need to do anything else?
I'm pretty sure the Windows command line pipeline is just choking on your previous command.
I think the best chance you have is doing this using PyPy:https://pypi.org/project/pymssql/, given the SQL instance has the memory to handle the data stream.

Running SQL Files with Variety of DDL commands and INSERT operations from a VB6 Code

I have a VB6 application, which restores a BAK file to the Database and runs through some scripts.
As there are large volume of SQL Scripts to run, I decided to move all in to a SQL File and then to run it from the Application.
The SQL File is now filled with lots of CREATE, UPDATE Table statements and also INSERT statements. I use both the SQL Server 2008 R2 and SQL Server 2012.
Is there a way or function to execute the same from the VB6 code?
Thanks in advance for the help.
EDIT 1: The SQL File also have the CREATE Procedures and Triggers
You can run SQL scripts from the command line using
sqlcmd -S myServer\instanceName -i C:\myScript.sql
(see https://msdn.microsoft.com/en-us/library/ms170572.aspx)
In VB6 you can execute command line strings using the Shell command, for example to start the Windows Calculator:
Shell "C:\WINDOWS\System32\calc.exe", vbNormalFocus
For more on the Shell command see: https://msdn.microsoft.com/en-us/library/aa242087.aspx and How do I call a Windows shell command using VB6?
The combination should give you what you need, like this:
Shell "sqlcmd -S myServer\instanceName -i C:\myScript.sql", vbNormalFocus
I see there is a good amount of useful information about SQLCMD in this article https://www.simple-talk.com/sql/sql-tools/sql-server-sqlcmd-basics/ which may be a useful read.

OUTFILE Microsoft SQL Server equivalent?

Is there something like INTO...OUTFILE (from MySQL) in Microsoft SQL Server?
This code causes the query results to be dumped into a text file.
EXEC master..xp_cmdshell'bcp "SELECT TOP 5 CUSTOMERID FROM Northwind.dbo.Customers" queryout "c:\text.txt" -c -T -x'
References:
Code Samples
BCP Utility Reference
xp_cmdshell Reference
You can also use the sqlcmd or osql (in SQL 2000) to store the output from a command line outside of SSMS very like working with mysql from the command line. You just have to provide the correct parameters, including the query you want to run.
It depends on how fancy you want to get. You can use SSMS to generate a CSV A less user-friendly option is a BCP export. Which I see #hamlin11 just suggested

SQL delphi ado, SQL batch execute

Since SQL Server does not have a simple batch command line executor for the scripts the are auto generated from management studio, I created one.
The problem arises when delphi ado syntax and SQL Server syntax don't agree (BUT ITS THE SAME THING).
Well any how, the go I replaced with ;
Now as I declare a stored procedure alter, I hit a brick wall.
The script I'm running is :
ALTER Procedure [dbo].[procName]
as
Declare #param int
and the error i get is :
the arguments are from the wrong type,
out of range or collide with one
another.
(my free translation)
questions :
why is this happening?
what can i do to change this?
is there another udl based program that parse SQL scripts?
thanks.
edit: require login to the db with udl file.
could it be that delphi has problems with # ?
Since SQL Server does not have a
simple batch command line executer for
the scripts the are auto generated
from management studio, I created one.
Are you aware of SQLCMD ?? Seems like a command-line utility to execute SQL scripts to me... also: the SQLCMD utility has a number of additional enhancements that go beyond what the T-SQL scripts in SSMS can do.
Also check out:
SQLCMD reference
Using SQLCMD utility
Not sure about your SQL example above, but does the stored procedure actually have any parameters, or are you calling a variable inside the body #param? The usual syntax is:
ALTER Procedure [dbo].[procName]
(<#params here>)
AS
<body + variables here>
MSDN - Alter Procedure
removed the component and change the code from
ado.open;
to
ado.execute;//or something like this
solved it.

No way to execute SQL script from SQL Server Query Manager like #{file.sql} oracle sqlplus syntax?

Like the title says, in oracle you can issue the following command in SQL*Plus:
SQL> select something from anothertable; #sql
SQL> #{/home/me/somescript.sql}; #load sql from file and execute it
SQL> do something else in script; #other sql
Without having to file->open the sql script to load it to the UI.
Is there an equivalent in SQL Server Query Manager? I've stumbled upon many situation where i could have used it but i couldn't be able to find a way to accomplish it.
You're not really comparing like for like Tools here.
The equivalent tool to SQL*Plus in SQL Server is the SQLCMD Utility.
In particular you will be interested in the -i switch as this allows you to provide a .sql file as input.
Edit:
In response to your comment, you could look to use the system stored procedure xp_cmdshell to launch a prompt form within a T-SQL batch that allows you to use SQLCMD. Not the most elegant solution in my opinion but it should work.
If using latter versions of SQL Server (2005 & 2008), see if the :r command in SQLCMD works for you:
:r <filename>
Parses additional Transact-SQL statements and sqlcmd commands from the file specified by <filename> into the statement cache.
If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the line that follows :r.
From sqlcmd Utility
Use isql utility http://msdn.microsoft.com/en-us/library/aa214007(SQL.80).aspx
issql ... -iinputfile
There is also SQLS*Plus tool that you can use to execute scripts within a script