TipsOnLips.net

Your .net tips and tricks source

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010


SQL Server report - printing size formula

When creating a SQL Server report and the layout is larger than it fits on an A4 or A3 page, then printing to PDF is the only option to make printing successful.

Adobe PDF format is scalable, i.e. it does not get restricted by the A4 or A3 size as long things are proportional. To explain that:

You set the report size in the report properties to A3 size is 42cm x 29.7 (for large reports) and then check the report body size, if that for example is 43.5 cm, then

the new report size to be set is (43.5 (body size) x 42 (report size)) / (report size - left margin - right margin). Then when you print this report in PDF, you will not get an extra page with only extra column.

The good thing about this way is now you can print in any paper size without any problem.

Let me know if that works for you.


Categories: SQL Server
Posted by developer on Thursday, August 20, 2009 9:01 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Kill All Blocked Transactions On SQL Server

--Kill All Blocked Transactions

use master

DECLARE @pid varchar(255)

DECLARE MY_CURSOR Cursor FOR

select distinct req_transactionUOW

from syslockinfo

WHERE req_spid=-2 and req_transactionUOW <> '00000000-0000-0000-0000-000000000000'

OPEN MY_CURSOR

FETCH FROM MY_CURSOR

INTO @pid

WHILE @@FETCH_STATUS = 0

BEGIN

 

PRINT 'KILL ''' + @pid + ''''

exec ('KILL ''' + @pid + '''')

FETCH FROM MY_CURSOR

INTO @pid

END

CLOSE MY_CURSOR

DEALLOCATE MY_CURSOR


Categories: SQL Server
Posted by developer on Monday, February 23, 2009 7:26 AM
Permalink | Comments (5) | Post RSSRSS comment feed

Some useful date processing SQL Snippets

---Calculates the first day of the previous month

SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0)

AS [First day of the previous month]

---Calculates the first day of current month

SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)

AS [First day of the current month] [more]

---Calculates the first day of next month

SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0)

AS [First day of the next month]

---Calculates the last day of the previous month

SELECT DATEADD(d, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))

AS [Last day of the previous month]

---Calculates the last day of the current month

SELECT DATEADD(d, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))

AS [Last day of the current month]

--Calculates the last day of the next month

SELECT DATEADD(d, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 2, 0))

AS [Last day of the next month]

--Calculates the first day of the year

SELECT DATEADD(yy, DATEDIFF(yy,0,GetDate()), 0)

AS [First day of the year]

--Calculates the first day of the quater

SELECT DATEADD(qq, DATEDIFF(qq,0,GetDate()), 0)

AS [First day of the quarter]

--Calculates the first monday of the month

SELECT DATEADD(wk, DATEDIFF(wk,0,dateadd(dd, 6 - DATEPART(Day,GetDate()),GetDate())), 0)

AS [First monday of the month]

--Calculates the last day of the prior month

SELECT DATEADD(mm, DATEDIFF(mm,0,GetDate()), 0)

AS [Last day of the previous month]

--Calculates the last day of the prior year

SELECT DATEADD(yy, DATEDIFF(yy,0,GetDate()), 0)

AS [Last day of the previous year]

--Calculates the last day of the current year

SELECT DATEADD(mm, DATEDIFF(m,0,GetDate() ) + 1, 0)

AS [Last day of the current year]

--Calculates the monday of the current week

SELECT DATEADD(wk, DATEDIFF(wk,0,GetDate()), 0)

AS [Monday of the current week]

--Calculates the yesterdays date

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), -1)

AS [Yesterdays date]

--Calculates the todays date

SELECT GetDate()

AS [Todays date]

--Calculates the tommorows date

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 1)

AS [Tommorows date]

---Calculates the 15th day of previous month

SELECT DATEADD(d, 14, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1 , 0))

AS [15th day of previous month]

---Calculates the 15th day of current month

SELECT DATEADD(d, 14, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))

AS [15th day of current month]

---Calculates the 15th day of next month

SELECT DATEADD(d, 14, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))

AS [15th day of next month]

--Gets the name of the current month

SELECT DATENAME(month, GetDate())

AS [Name of the current month]

--Gets the weekday name of the current date

SELECT DATENAME(dw, GetDate())

AS [Weekday name of the current date]

--Gets the weekday name of the current date next year

SELECT DATENAME(dw, DATEADD(yy, 1, GetDate()))

AS [Weekday name of the current date next year]

--Gets the weekday name of the current date last year

SELECT DATENAME(dw, DATEADD(yy, -1, GetDate()))

AS [Weekday name of the current date last year]

 

First day of the previous month

-------------------------------

2008-09-01 00:00:00.000

(1 row(s) affected)

First day of the current month

------------------------------

2008-10-01 00:00:00.000

(1 row(s) affected)

First day of the next month

---------------------------

2008-11-01 00:00:00.000

(1 row(s) affected)

Last day of the previous month

------------------------------

2008-09-30 00:00:00.000

(1 row(s) affected)

Last day of the current month

-----------------------------

2008-10-31 00:00:00.000

(1 row(s) affected)

Last day of the next month

--------------------------

2008-11-30 00:00:00.000

(1 row(s) affected)

First day of the year

-----------------------

2008-01-01 00:00:00.000

(1 row(s) affected)

First day of the quarter

------------------------

2008-10-01 00:00:00.000

(1 row(s) affected)

First monday of the month

-------------------------

2008-10-06 00:00:00.000

(1 row(s) affected)

Last day of the previous month

------------------------------

2008-10-01 00:00:00.000

(1 row(s) affected)

Last day of the previous year

-----------------------------

2008-01-01 00:00:00.000

(1 row(s) affected)

Last day of the current year

----------------------------

2008-11-01 00:00:00.000

(1 row(s) affected)

Monday of the current week

--------------------------

2008-10-27 00:00:00.000

(1 row(s) affected)

Yesterdays date

-----------------------

2008-10-28 00:00:00.000

(1 row(s) affected)

Todays date

-----------------------

2008-10-29 13:14:31.130

(1 row(s) affected)

Tommorows date

-----------------------

2008-10-30 00:00:00.000

(1 row(s) affected)

15th day of previous month

--------------------------

2008-09-15 00:00:00.000

(1 row(s) affected)

15th day of current month

-------------------------

2008-10-15 00:00:00.000

(1 row(s) affected)

15th day of next month

-----------------------

2008-11-15 00:00:00.000

(1 row(s) affected)

Name of the current month

------------------------------

October

(1 row(s) affected)

Weekday name of the current date

--------------------------------

Wednesday

(1 row(s) affected)

Weekday name of the current date next year

------------------------------------------

Thursday

(1 row(s) affected)

Weekday name of the current date last year

------------------------------------------

Monday

(1 row(s) affected)

 

Source: http://www.keithrull.com/2008/07/24/SomeUsefulDateProcessingSQLSnippets.aspx


Categories: SQL Server
Posted by developer on Wednesday, October 29, 2008 6:12 AM
Permalink | Comments (0) | Post RSSRSS comment feed

SQL Server Database Samples

1) AdventureWorks

2) Northwind and pubs databases


Categories: SQL Server
Posted by developer on Thursday, October 02, 2008 10:34 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Scope_Identity and NOCOUNT (ADO and SQL Server)

When we have the following ADO.NET code:

int returnValue = command.ExecuteNonQuery();

which calls a SQL Server stored procedure that includes this statement:

NOCOUNT ON

The returnValue will always be -1 if the NOCOUNT was set to ON, therefore we need to remove the

NOCOUNT ON to get the number of records affected from the Command.ExecuteNonQuery().


Categories: SQL Server
Posted by developer on Friday, February 01, 2008 7:59 AM
Permalink | Comments (0) | Post RSSRSS comment feed

SQL Server - Reset Primary Key

When deleting all records from a SQL Server table, you may need to reset the primary key to start from

zero again when inserting new rows. Just execute the following script:

DBCC CHECKIDENT('TableName', RESEED, 0)


Categories: SQL Server
Posted by developer on Friday, December 14, 2007 3:20 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Add a database diagram support to a SQL Server 2000 database

When attaching a SQL Server 2000 database to a 2005 SQL Server, the 2005 IDE would not support opening the 2000 database diagrams. But there are two ways to make this happens: 1) In SQL Server 2005 Management Studio do the following:

  1. Right Click on your database, choose properties
  2. Goto the Options Page
  3. In the Dropdown at right labeled "Compatibility Level" choose "SQL Server 2005(90)" [more]

 

2) Or by script:

      EXEC sp_dbcmptlevel 'EnterYourDatabaseNameHere', '90';

 

Also, if you get this error when trying to add a new Diagram:

Error: Database diagram support objects cannot be installed because this database does not have a valid owner

Execute the following script:

EXEC sp_dbcmptlevel 'YourDB, '90';

go

ALTER AUTHORIZATION ON DATABASE::YourDB TO "sa"

go

 

 


Categories: SQL Server
Posted by developer on Friday, August 31, 2007 9:50 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Get the Identity or Row number value for a SQL server table row

We can retrieve a row number or Identity of a SQL Server table row using the script below:

select identity(int) col INTO #Temp from dbo.table

select * from #Temp

Idenity is retrieved when INTO is used to insert the record into a user or temporary table  

Categories: SQL Server
Posted by developer on Wednesday, August 29, 2007 6:34 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Find an SQL Server database size and table space

To get details about an SQL Server database using the following stored procedure:       

       sp_helpdb 'DatabaseName'

to get help about an SQL Server table, use the following stored procedure:        

       sp_help 'TableName'

 To get details about an SQL Server table and indexes siz, using the following stored procedure:

        sp_spaceused 'TableName'

 ----------------------------------------------------------------------------------------------------

Also, to list all tables that contain a given column name (Source: http://www.cryer.co.uk/brian/sqlserver/sqlsvrhowto.htm)

select sysobjects.name, * from syscolumns, sysobjects

where syscolumns.name like '%MyColumn%'

and sysobjects.id = syscolumns.id

and (sysobjects.xtype='U' or sysobjects.xtype='S')

------------------------------------------------------------------------

To kill all the processes for a specific database, use the following script:

declare @p_DBName sysname

set @p_DBName = 'DatabaseName'

/* Must be run from master */

USE master

/* Check Paramaters */

/* Check for a DB name */

IF (@p_DBName IS NULL)

BEGIN

PRINT 'You must supply a DB Name'

RETURN

END -- DB is NULL

IF (@p_DBName = 'master')

BEGIN

PRINT 'You cannot run this process against the master database!'

RETURN

END -- Master supplied

SET NOCOUNT ON

/* Declare Variables */

DECLARE @v_spid INT,

@v_SQL NVARCHAR(255)

/* Declare the Table Cursor (Identity) */

DECLARE c_Users CURSOR

FAST_FORWARD FOR

SELECT spid

FROM master..sysprocesses (NOLOCK)

WHERE db_name(dbid) = @p_DBName

OPEN c_Users

FETCH NEXT FROM c_Users INTO @v_spid

WHILE (@@fetch_status <> -1)

BEGIN

IF (@@fetch_status <> -2)

BEGIN

SELECT @v_SQL = 'KILL ' + convert(NVARCHAR, @v_spid)

-- PRINT @v_SQL

EXEC (@v_SQL)

END -- -2

FETCH NEXT FROM c_Users INTO @v_spid

END -- While

CLOSE c_Users

DEALLOCATE c_Users


Categories: SQL Server
Posted by developer on Wednesday, August 29, 2007 9:47 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Drop all triggers from any SQL Server database

To drop all triggers from any database, execute this script:

declare @trigger varchar(1000)

declare @sql nvarchar(1000)

select @trigger = min([name]) from sysobjects where [name] like 'trig_prefix%'

while @trigger is not null [more]

begin

      set @sql = 'drop trigger ' + @trigger

      exec sp_executesql @sql

      select @trigger = min([name]) from sysobjects where [name] like 'trig_mi%' and [Name] > @trigger
end


Categories: SQL Server
Posted by Admin on Monday, August 27, 2007 9:21 AM
Permalink | Comments (5) | Post RSSRSS comment feed