Thursday, January 28, 2016

SQL Server Databsae from SINGLE USER mode to MULTI USER

 
SQL Server 2012 
Right Click on  DB > Properties > Options > [Scroll down] State > RestrictAccess > select Multi_user and click OK.
SQL Server 2008 R2 and earlier
Script to change the database from single user to multi user
--Script
use
master
GO
-- Find the active sessions and kill it
select DB.name, DB.dbid, spid, login_time, nt_domain, nt_username, loginame 
from sysprocesses Process
inner join sysdatabases DB
on Process.dbid = DB.dbid
where DB.name = 'Databsae Name'
GO
kill spid
--Execute the below stored procedure to change to multiuser
exec sp_dboption 'Database Name', 'single user', 'FALSE'

Friday, January 22, 2016

GO statement in SQL

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.

The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command.

declare @a int

select @a=100

print @a

GO -- @MyMsg is not valid after this GO ends the batch.

print @a

one more use of GO

The batch preceding GO will execute the specified number of times.

with the GO , below insert query runs 100 times.

create table test(a integer)

insert into test values(1)

go 100

Thursday, January 7, 2016

Script to generate QVD's


Please find the below script:

 

//DB Conncetion

SET DayNames='Mon;Tue;Wed;Thu;Fri;Sat;Sun';

 

// #### DB COnnection #### This will be change if we have more DB's

OLEDB CONNECT TO [ConnectionString];

// ##### QVD Path ####### This will be change if we want to change the QVD folder path

SET vQVDFolderPath = 'qvdata' ;

 

// List of Tables, Here I am creating with InLine. You can Use txt file or excel file. This will be change

LIST_OF_TABLES:

LOAD * INLINE [

TABLENAME

SCHEMA.TABLE1

SCHEMA.TABLE2

] ;

// From This line, the script is static

// generate the Loop for each value in the LIST_OF_TABLES table

 

FOR i=1 to FieldValueCount('TABLENAME')

LET vTableName = FieldValue('TABLENAME',$(i)) ;

 

$(vTableName):

SELECT * FROM $(vTableName) ;

 

STORE $(vTableName) into [$(vQVDFolderPath)\$(vTableName).qvd] (qvd);

 

//QVD DETAILS info

QVDDETAILS:

LOAD '$(vTableName).qvd' AS QVD_NAME ,

       '$(vTableName)' AS SOURCE_TABLENAME,

       NoOfRows('$(vTableName)') AS NO_OF_ROWS

AutoGenerate 1 ;

 

DROP Table $(vTableName) ;

 

NEXT i

 

In the above script, just change the DB connection , QVD Path and List of Tables for the another QVD Generator qvw file.

 

Wednesday, January 6, 2016

List of SSIS Packages based on keyword search

SQL Query to find the SSIS Packages with wildcard search for specific keyword.

For SQL Server 2008 and above

SELECT name
,CAST(CAST([packagedata] as varbinary(max)) as xml) AS PackageSource
FROM [msdb].[dbo].[sysssispackages]
WHERE CAST(CAST([packagedata] AS VARBINARY(MAX)) AS VARCHAR(MAX))
LIKE '%keyword%'

For SQL Server 2005:

SELECT name, CAST(CAST([packagedata] AS VARBINARY(MAX)) AS VARCHAR(MAX))

FROM msdb.dbo.sysdtspackages90

WHERE CAST(CAST([packagedata] AS VARBINARY(MAX)) AS VARCHAR(MAX))

LIKE '%keyword%'



SELECT name
,CAST(CAST([packagedata] as varbinary(max)) as xml) AS PackageSource
FROM [msdb].[dbo].[sysssispackages]
WHERE CAST(CAST([packagedata] AS VARBINARY(MAX)) AS VARCHAR(MAX))
LIKE '%keyword%'

For SQL Server 2005:

SELECT name, CAST(CAST([packagedata] AS VARBINARY(MAX)) AS VARCHAR(MAX))

FROM msdb.dbo.sysdtspackages90

WHERE CAST(CAST([packagedata] AS VARBINARY(MAX)) AS VARCHAR(MAX))

LIKE '%keyword%'