Archive
How to retrieve last 12 months records form a table – MSDN TSQL forum
–> Question:
I have a data field in my table that i would to use within my WHERE clause, to get the last 12 months dataset.
So for example, WHERE date between ‘20110323’ and ‘20100323’
Is this possible? I know I have to use something like GETDATE() or something but not quite sure how to incorporate this.
–> My Answer:
You can make your WHERE clause like this:
WHERE date_field between getdate() and getdate()-365 -- OR WHERE date_field between getdate() and dateadd(m,-12,getdate()
–> Answer by Uri Dimant:
You can make your WHERE clause like this:
SELECT * FROM tbl WHERE dt >= DATEADD(month,-12,DATEADD(day,DATEDIFF(day,0,GETDATE()),0)) AND dt <=DATEADD(day,DATEDIFF(day,0,GETDATE()),0)
Ref link.
Using OUTPUT Parameters in Stored Procedures
According to MS-BOL, SQL Server Stored-Procedures can return data in 4 forms:
1. Return Code: which are always an integer value.
2. OUTPUT Parameter: which can return either data (such as an integer or character value) or a cursor variable (cursors are result sets that can be retrieved one row at a time).
3. A result set for each SELECT statement contained in the stored procedure or any other stored procedures called by the stored procedure.
4. A global cursor that can be referenced outside the stored procedure.
Specifying OUTPUT keyword to the parameters in the Stored Procedures can return the values of the parameters to the calling program.
Let’s check this with a simple example by using AdventureWorks database:
USE [AdventureWorks] GO --// Create Stored Prcedure with OUTPUT parameter CREATE PROCEDURE getContactName @ContactID INT, @FirstName VARCHAR(50) OUTPUT, @LastName VARCHAR(50) OUTPUT AS BEGIN SELECT @FirstName = FirstName, @LastName = LastName FROM Person.Contact WHERE ContactID = @ContactID end GO --// Test the Procedure DECLARE @CID INT, @FName VARCHAR(50), @LName VARCHAR(50) --/ Test# 1 SET @CID = 100 EXEC getContactName @ContactID=@CID, @FirstName=@FName OUTPUT, @LastName=@LName OUTPUT SELECT @FName as 'First Name', @LName as 'Last Name' --/ Output -- ContactID First Name Last Name -- 100 Jackie Blackwell --/ Test# 2 SET @CID = 200 EXEC getContactName @ContactID=@CID, @FirstName=@FName OUTPUT, @LastName=@LName OUTPUT SELECT @FName as 'First Name', @LName as 'Last Name' --/ Output -- ContactID First Name Last Name -- 200 Martin Chisholm GO --// Final Cleanup DROP PROCEDURE getContactName GO
>> Check & Subscribe my [YouTube videos] on SQL Server.
EXECUTE (or EXEC) vs sp_executesql
–> EXECUTE:
As per MS BOL EXECUTE executes a command string or character string within a TSQL batch, or one of the following modules: system stored procedure, user-defined stored procedure, scalar-valued user-defined function, or extended stored procedure. The TSQL query can be a direct string or a variable of char, varchar, nchar, or nvarchar data type.
–> sp_executesql:
As per MS BOL sp_executesql executes a TSQL statement or batch that can be reused many times, or one that has been built dynamically. The TSQL statement or batch can contain embedded parameters. The SQL query is a Unicode string or a Unicode variable that contains a Transact-SQL statement or batch. Here the variable datatype is restricted to Unicode nchar or nvarchar only. If a Unicode constant (SQL string) is used then the it must be prefixed with N.
–> Main difference performance wise: sp_executesql is generally preferred over EXEC() when executing dynamic T-SQL. sp_executesql works by creating a stored procedure using the specified query, then calling it using the supplied parameters. Unlike EXEC(), sp_executesql provides a mechanism that allows you to parameterize dynamic T-SQL and encourage plan reuse. A dynamic query that is executed using sp_executesql has a much better chance of avoiding unnecessary compilation and resource costs than one ran using EXEC().
Let’s check the difference with a simple example using [AdventureWorks] database’s [Person].[Contact] table:
USE [AdventureWorks] GO DECLARE @str NVARCHAR(1000) DECLARE @fName NVARCHAR(20) DECLARE @ePromo INT SET @fName = N'James' SET @ePromo = 1 --// Using EXECUTE or EXEC SET @str = N' SELECT ContactID, FirstName, MiddleName, LastName, EmailAddress, Phone, EmailPromotion FROM Person.Contact WHERE FirstName = ''' + @fName + ''' AND EmailPromotion = ' + CAST(@ePromo as NVARCHAR(20)) PRINT @str EXEC (@str) GO --// Using sp_executesql DECLARE @str NVARCHAR(1000) DECLARE @fName NVARCHAR(20) DECLARE @ePromo INT DECLARE @paramList NVARCHAR(500) SET @paramList = N'@fNameParam NVARCHAR(20), @ePromoParam INT' SET @str = N' SELECT ContactID, FirstName, MiddleName, LastName, EmailAddress, Phone, EmailPromotion FROM Person.Contact WHERE FirstName = @fNameParam AND EmailPromotion = @ePromoParam' SET @fName = N'James' SET @ePromo = 1 EXECUTE sp_executesql @str, @paramList, @fNameParam=@fName, @ePromoParam=@ePromo -- When the match is LIKE SET @str = N' SELECT ContactID, FirstName, MiddleName, LastName, EmailAddress, Phone, EmailPromotion FROM Person.Contact WHERE FirstName LIKE ''%'' + @fNameParam + ''%'' AND EmailPromotion = @ePromoParam' SET @fName = N'James' SET @ePromo = 0 EXECUTE sp_executesql @str, @paramList, @fNameParam=@fName, @ePromoParam=@ePromo GO
Security Note:
Before you call EXECUTE or sp_executesql with a character string, validate the character string. Never execute a command constructed from user input that has not been validated. For more information, see SQL Injection.
SQL DBA – SQL Queries Executed, Running & Blocking
Here are some TIPS for checking what all SQL statements:
– were executed recently.
– currently running in background.
– blocking or deadlocking.
USE [AdventureWorks] GO --// Which SQL query or statement was executed recently? -- Execute the Stored Proc with different parameters & some SQL queries or statements. EXEC uspGetEmployeeManagers 1 GO EXEC uspGetEmployeeManagers 2 GO SELECT * FROM Person.Contact WHERE ContactID = 100 GO SELECT * FROM HumanResources.Employee WHERE EmployeeID = 200 GO -- Following SQL query will get you which SQL statement or Object was executed/used at what time and how many times. SELECT object_name(b.objectid) AS obj_name, a.execution_count, a.last_execution_time, b.text FROM sys.dm_exec_query_stats a CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) AS b --where text like '%uspGetEmployeeManagers%' ORDER BY 2 DESC GO
--// Who's executing what SQL statements. sp_who2 GO -- You can check any SPID that is greater than 50. This will give you the SQL statement executing against that SPID. -- Also check if any process is blocking any other process, the BlkBy column will give you the process SPID thats blocking the current process. Put that blocking SPID as paremeter in DBCC INPUTBUFFER() statement and get the SQL statement which has blocked your process. DBCC INPUTBUFFER (51) GO -- You can also Kill the process by providing the blocking SPID as parameter with the KILL statement. KILL 51 GO
More about:
– UDF(TVF) sys.dm_exec_sql_text
– View sys.dm_exec_query_stats
SQL DBA – When was the Table, View, Stored Procedure Created or Modified
On my previous blog posts [link] I discussed about System Catalog views and some tips to get the informaion regarding Database objects, like: Tables, Views, etc.
Adding to that post, this post provides the basic information that various DBAs & Developers are interested in, i.e. when was the Table created? or When was that particular Stored Procedure modified or updated?
Following set of queries provides this information:
select object_id, name, create_date, type_desc, modify_date from sys.tables UNION select object_id, name, create_date, type_desc, modify_date from sys.views UNION select object_id, name, create_date, type_desc, modify_date from sys.procedures UNION select object_id, name, create_date, type_desc, modify_date from sys.triggers
-OR- a single query to check all objects information:
select object_id, name, create_date, type_desc, modify_date
from sys.all_objects
where type in ('U', 'V', 'P', 'TR' ,'FN', 'IF', 'TF')
order by type, name
By querying the sys.all_objects view you can also get the information about other DB objects other than listed above, like: Indexes, Constraints, Synonyms, Stats, etc.
MS BOL link on sysobjects and object-type codes: http://msdn.microsoft.com/en-us/library/ms177596.aspx





