I often create batch files to execute sql scripts ( specially SPs) and then schedule it using SQL agent job/Task Scheduler. In this post , I will show you how to create a batch file with sql commands.
First of all, The command that is used to execute SQL queries from commnd prompt is “SQLCMD“. The general syntax of this command is available in the link. I will go with a simple one as below
sqlcmd –E –S Servername –Q Query -o “Output Path”
here
-E = Trusted connection (windows authentication)
-S = Server Name
-Q = CommandLineQuery
-o = Output file path
a simple command on the commnad prompt can be
C:\Users\geekEpisodes> sqlcmd -E -S “Local-SRV01″ -Q “Select top 10 * from MyTestDatabase..[ForecastHistory]” -o “C:\New Folder\Output.txt”
All the commands that you execute in the command prompt can be executed from batch/command files. to create a batch file with SQL commands follow the steps below
Read more…
Categories: SQL Queries, SQL Server, SQL Server, Stored Procedures, String Functions, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: command prompt, sql queries, SQL Server, sqlcmd, T-SQL
November 18th, 2010
admin
RAISERROR statement is used to return error messages to the business applications that executes SQL statements. The usual errors returned by the SQL server may not make much sense to the business applications users hence we overwrite it by using RAISERROR to display meaningful messages. The statement uses same format as a system error or warning messages generated by SQL server.
You can return a user-defined error message by using RAISERROR. The general syntax is as below
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
ExamplesRAISERROR (‘Error raised in TRY block.’, — Message text.
Read more…
Categories: Error Messages, Errors, Functions and DMVs, Operators, SQL Queries, SQL Server, T-SQL, T-SQL Tags: errors, functions, raiserror, sql queries, SQL Server, T-SQL, try catch
November 14th, 2010
admin
Logical operators can be used in the queries to retrieve records based on one more conditions. In a select statement, the conditions specified by the logical operators are connected using a WHERE clause.
General syntax :
Select ColumnList
From TableName
Where ConditionalExpr1 {AND/OR} [NOT] ConditionalExpr2
There are 3 types of logical operators
Read more…
Categories: Operators, Operators, SQL Queries, SQL Server, T-SQL, T-SQL Tags: and, logical operators, not operator, operators, or, sql queries, SQL Server, T-SQL