Skip to main content

Posts

Showing posts from September, 2007

SQLCMD -- Part VI (Scripting Variables)

To list all available SQLCMD Scripting variables, do the following: Step 1: Go to DOS prompt and open up SQLCMD Step 2: type :listvar which would list all SQLCMD scripting variables. In the screenshot you can see that the SQLCMDEditor and SQLCMDINI variable which we overwrote in the previous posts here and here are displayed. Almost all of the other variables are self-explanatory :) Creating our own local variables: :setvar DB1 testBed :setvar DB2 Adventureworks use $(DB1) go Select getdate(); go use $(DB2) go select top 5 city from person.address go Point to note: 1. If you execute :listvar again you can find these newly created variables getting listed there now. But please note that these local variables would be alive only till the life of the current session. i.e., If you exit out of SQLCMD once and come back and type :listvar these variables would be missing there. 2. Always the variables which we create will be in Uppercase only . Just try creating something like this: :s

SQLCMD -- Part V (setting startup scripts)

There are instances where we might need to run some default scripts on a specified server once SQLCMD gets connected. It can be achieved in just three steps as explained below: Step 1: Create a script file which you wanted to fire when SQLCMD gets connected to your SQL Server. For keeping the example simple, I used the following line and saved it as SqlCmdStartUpScripts.sql print 'Welcome today''s date is: ' + (convert(varchar, getdate())) Step 2: Open DOS prompt and type set sqlcmdini=c:\vadivel\sql related\scripts\SqlCmdStartUpScripts.sql Step 3: Then type SQLCMD and press Enter . Refer the below screenshot for the sample output.

SQLCMD -- Part IV (set your favourite editor)

From my previous posts one can understand that it is possible to write SQL queries directly in command prompt with the help of SQLCMD utility. Now let's assume we have typed a 'big' query and there is a typo there! Instead of going back and forth to edit it in command prompt won't it be easy if we are able to open the query in an editor and make the corrections there? Yes its possible in SQLCMD. All we need to do is type ed and it will open up the last command/query in a text editor. FYI the default editor is Edit (the command line editor of MS DOS). Step 1: Open up SQLCMD and connect to your SQLServer Step 2: type any query of your choice Step 3: type ed Step 4: The query would have opened in the 'EDIT' utility of DOS. Once you are done with the change, save and exit from that. Step 5: type go and press enter Can I make 'ed' to open up notepad or any editor of my choice? Yes it's possible. Step 1: Open DOS prompt Step 2: Type set sqlcmdedito

SQLCMD -- Part III (Non-Interactive or batch Mode)

If you haven't gone through the first two posts about SQLCMD I would strongly recommend to go over it here and here before proceeding further :) 1. Executing a script file from SQLCMD... i. Create a script file by typing in the following line and save it as Message.sql print 'Welcome today''s date is: ' + (convert(varchar, getdate())) ii. Now goto command prompt and type: SQLCMD -i Message.sql here, -i is the switch to specify the input file name. 2. Executing series of script files (sample) Lets create couple of .sql files and then see how to execute them in order from command prompt. Please note that i am just showing an example here :) there are better methods of doing the same which I would explain later in the series! i) Copy paste the below script and name it as 01TableCreation.sql Create table tblTest ( Sno int identity, FName varchar(20) ) Go ii) Copy paste the below script and name it as 02InsertRecords.sql Insert into tblTest (Fname) values ('a'

SQLCMD -- Part II (Interactive Mode)

First get connected into the DB Server using SQLCMD using either the windows authentication or SQL authentication as explained in the previous post . Then on the prompt you can type in the TSQL queries directly and press enter. In the next line, say Go and press enter to execute the query and see the result. See below an example to display the current datetime. After displaying the result the cursor would stand on the prompt expecting for further queries from us :) Once you are done you can 'exit' out of sqlcmd utility like shown below: By default, it get's connected to the default database of that login only. One can make use of -d switch to connect to a DB of their choice . Please refer the below sample where I make use of Adventureworks DB and query a table. What is the difference between -q and -Q switch? -q is for running queries from SQLCMD. -Q is for exitting from SQLCMD immediately after executing a given query. SQLCMD -q "Select getdate()" This query

SQLCMD -- Part I (Basics, Connectivity)

“SQLCMD” is a command line tool which was shipped by Microsoft along with SQL Server 2005. Previously SQL Server was having ISQL and OSQL as its command line utility. SQLCMD is replacing both of them (i.e., ISQL is not there in SQL Server 2005 RTM version. OSQL would also be eventually removed!!). When “SQLCMD” is run from the MS-DOS command prompt, it uses the OLE DB provider to execute the given queries. SQLCMD has batch and interactive modes. 'Batch mode' can be used mainly for scripting and automation tasks, while 'Interactive mode' is for firing ad-hoc querys. i) To list all the parameters supported by SQLCMD utility, run the following in command prompt SQLCMD /? ii) To connect to SQL Server using SQL Authentication SQLCMD -U username -P yourpassword -S Servername Once we press the enter key. If the UID/PWD is valid it would prompt you to enter the t-sql query to execute. If at all the UID/PWD combination is wrong it would throw an error similar to the one shown be

NEWID vs NEWSEQUENTIALID

Some pointers: 1. NewSequentialID() and NewID() both generates the GUID of datatype of uniqueidentifier. 2. NewID() generates the GUID in random order 3. NewSequentialID() generates the GUID in sequential order. 4. NewSequentialID() can be used ONLY in default clause of a table. 5. NewSequentialID() is easily predictable 6. So if security is a major concern then go for NewID() instead of NewSequentialID(). Example to demonstrate NEWSEQUENTIALID(): Create table #tblDemoSequentialID ( Column1 uniqueidentifier default NewID(), Column2 uniqueidentifier default NewSequentialID(), Fname varchar(30) ) Pump-in few dummy records: Insert into #tblDemoSequentialID (Fname) values ('Vadivel') Insert into #tblDemoSequentialID (Fname) values ('Rajinikanth') Insert into #tblDemoSequentialID (Fname) values ('Sivaji') In this query 'Column1' would demonstrate that the 'NEWID' has generated GUID in random fashion. Also 'Column2' would contain GUID in Sequen