TSQL: excute a SSIS/dts package in a stored procedure bypassing parameters

DECLARE @returncode int
DECLARE @cmd varchar(4000)
DECLARE @ImgURL varchar(250)
DECLARE @ImageToolPath varchar(250)
DECLARE @Msg varchar(500)
SET @ImgURL = ‘http://www.webdatenblatt.de/cds/de/?pid=f822e0c1E_’
SET @ImageToolPath = ‘C:\Daten\imgtest’

 

–execute ssis package with parameters

SELECT @cmd = ‘dtexec /f  “C:\SSIS\GetImages.dtsx”‘
+ ‘ /SET \Package.Variables[ImgURL].Properties[Value];’ + @ImgURL
+ ‘ /SET \Package.Variables[ImageToolPath].Properties[Value];’ + @ImageToolPath
EXEC @returncode = xp_cmdshell @cmd
IF @ReturnCode <> 0
BEGIN
SELECT @Msg = ‘SSIS package execution failed GetImages.dtsx on SQL Server\Instance: ‘ + QUOTENAME(@@servername,'”‘)
EXEC msdb.dbo.sp_send_dbmail @profile_name=’SQLServerMail’, @recipients = ‘xy@xy.xy’ , @body = @Msg, @subject = ‘SSIS Execution Failure’
END
ELSE
BEGIN
print ‘image download for import items finished’
END

Advertisements

TSQL: shrink all databases on SQL SERVER

 

IF EXISTS(select * from sysobjects where id = object_id(‘dbo.spShrinkAllDatabasesOnServer’) and xtype = ‘P’)
DROP PROCEDURE dbo.SP_ShrinkAllDatabasesOnServer
GO

 

CREATE PROCEDURE [dbo].[spShrinkAllDatabasesOnServer]
AS
BEGIN

 
CREATE TABLE #TempDatabasesTable

 

(
[DatabaseName] sysname not null primary key,
Mod tinyint not null default 1
)

 

INSERT INTO #TempDatabasesTable ([DatabaseName])
SELECT name FROM sys.databases WHERE database_id not in (1,2,3,4,5,6,7,8,9,94) order by database_id

 

 

 

DECLARE @DatabaseName sysname
SET @DatabaseName = ”
WHILE @DatabaseName is not null
BEGIN

 

SET @DatabaseName = NULL

 

SELECT TOP 1 @DatabaseName = [DatabaseName] from #TempDatabasesTable where Mod = 1

 

IF @DatabaseName is NULL BREAK

 
–wenn keine offenen Connections dann verkleinern
IF isnull((SELECT count(dbid) FROM sys.sysprocesses WHERE db_name(dbid)= @DatabaseName) , 0) = 0

 

BEGIN
PRINT ‘ ‘
PRINT ‘ ‘
PRINT ‘*******************************************************************’
PRINT ‘> DB: ‘ + @DatabaseName

 

DECLARE @SqlCommand nvarchar(4000)

EXEC(
‘ USE ‘ + @DatabaseName +
‘ EXEC(”
DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR LOCAL FOR
SELECT table_name FROM information_schema.tables WHERE table_type = ””base table””

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
Print ””Reindexing Table: ”” + @TableName
DBCC DBREINDEX(@TableName,”” ”” , 90)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
”)’)

PRINT ‘> SET RECOVERY MODE SIMPLE’

SET @SqlCommand = ‘ALTER DATABASE [‘ + @DatabaseName + ‘] SET recovery simple’
EXEC sp_executesql @SqlCommand

 

PRINT ‘> Shrinking database’

 

SET @SqlCommand = ‘dbcc shrinkdatabase([‘ + @DatabaseName + ‘])’
EXEC sp_executesql @SqlCommand

 

UPDATE #TempDatabasesTable set Mod = 0 where [DatabaseName] = @DatabaseName
END

END

 

DROP TABLE #TempDatabasesTable

 

END

 

TSQL: how to search content in all columns

DECLARE @SearchStr nvarchar(100)

SET @SearchStr = ‘USER_’

CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)

SET @TableName = ”

SET @SearchStr2 = QUOTENAME(‘%’ + @SearchStr + ‘%’,””)

 

WHILE @TableName IS NOT NULL

BEGIN

SET @ColumnName = ”

SET @TableName =

(

SELECT MIN(QUOTENAME(TABLE_SCHEMA) + ‘.’ + QUOTENAME(TABLE_NAME))

FROM INFORMATION_SCHEMA.TABLES

WHERE TABLE_TYPE = ‘BASE TABLE’

AND QUOTENAME(TABLE_SCHEMA) + ‘.’ + QUOTENAME(TABLE_NAME) > @TableName

AND OBJECTPROPERTY(

OBJECT_ID(

QUOTENAME(TABLE_SCHEMA) + ‘.’ + QUOTENAME(TABLE_NAME)

), ‘IsMSShipped’

) = 0

)

 

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

BEGIN

SET @ColumnName =

(

SELECT MIN(QUOTENAME(COLUMN_NAME))

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)

AND TABLE_NAME = PARSENAME(@TableName, 1)

AND DATA_TYPE IN (‘char’, ‘varchar’, ‘nchar’, ‘nvarchar’)

AND QUOTENAME(COLUMN_NAME) > @ColumnName

)

 

IF @ColumnName IS NOT NULL

BEGIN

INSERT INTO #Results

EXEC

(

‘SELECT ”’ + @TableName + ‘.’ + @ColumnName + ”’, LEFT(‘ + @ColumnName + ‘, 3630)

FROM ‘ + @TableName + ‘ (NOLOCK) ‘ +

‘ WHERE ‘ + @ColumnName + ‘ LIKE ‘ + @SearchStr2

)

END

END

END

SELECT ColumnName, ColumnValue FROM #Results