Execute SSIS in SSMS - sql-server

I have a SSIS package which will upload files into tables. I want to execute it as soon as a file has been uploaded and saved to a table. This link showed how to execute it using Stored Procedure. What I did was I created a trigger with the following code:
ALTER TRIGGER [dbo].[tr_ImportFile]
ON [dbo].[ReconMedicalAidFile]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
DECLARE #params VARCHAR(MAX),
#ssisStr VARCHAR(MAX),
#packageName VARCHAR(MAX),
#serverName VARCHAR(MAX)
SET #serverName = 'ServerName'
SET #packageName = 'MyIntegration'
SET #ssisStr = 'dtexec /sq ' + #packageName + ' /ser ' + #serverName
DECLARE #returnCode int
EXEC #returnCode = xp_cmdshell #ssisStr
END
I get the following error Procedure expects parameter 'command_string' of type 'varchar'.
Any help would be appreciated.
Thanks

I think it works when you replace VARCHAR(MAX) with VARCHAR(8000) data types

I went a different route. Still have the trigger but it will execute a SQL Job which contains the SSIS package.
EXEC msdb.dbo.sp_start_job N'JobName' ;

Related

Create database from a template by a script

I'm running a robot test on one of our test environments, and every night, before the test runs, I would like to dump the database, and create it again from a template.
I know that PSQL has a solution for it:
CREATE DATABASE name
[ TEMPLATE [=] template ]
Is there a way I can do this in SQL Server, and write a script for it?
You can write any kind of functionality as text and then execute it as a variable. That way you can store your template as text, where you would define the database.
declare #example nvarchar(max)
set #example = 'create database examplebase'
exec sp_executesql #example
You can use this in a stored procedure where you can set database name ect. as a parameter.
create PROCEDURE sp_recreateDB
-- Add the parameters for the stored procedure here
#databasename nvarchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
declare #example nvarchar(max)
SET #example = N'CREATE DATABASE ' + QUOTENAME(#Databasename) + N';'
-- ect...
exec sp_executesql #example
END
GO

Running statements in cursor returns Invalid object name error (SQL Server 2014)

I am trying to execute statements gained from a cursor, and I keep getting an "Invalid object name" error. To start with, the cursor pulls information from a table that has a database name in one column, and a SQL statement in another. It is defined as such:
DECLARE commands CURSOR FOR
SELECT
REPLACE(DBNAME, DBNAME, 'USE ' + DBNAME),
REPLACE(REPLACE(REPLACE(DESCRIPTION, 'OLDLINKEDSERVER', 'NEWLINKEDSERVER'), 'CREATE VIEW', 'ALTER VIEW'), 'CREATE VIEW', 'ALTER VIEW') AS CMD
FROM
#TMP2
Then, I define two commands:
DECLARE #cmd1 NVARCHAR(MAX)
DECLARE #cmd2 NVARCHAR(MAX)
So cmd1 is really just "USE DBName" where DBName is the name of the database, and cmd2 is a SQL statement to be run on that database.
For some reason, whenever I then iterate through the cursor:
OPEN Commands
FETCH NEXT FROM Commands INTO #cmd1, #cmd2
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC sp_executesql #cmd1
EXEC sp_executesql #cmd2
FETCH NEXT FROM Commands INTO #cmd1, #cmd2
END
CLOSE commands
DEALLOCATE commands
I get an error that says:
Msg 208, Level 16, State 6, Procedure NameOfView, Line 34
Invalid object name 'dbo.NameOfView'.
I've tried adding a "GO" command to cmd1, but it didn't work (I guess GO is only good for client tools). I tried using "exec(#cmd1)" instead of exec sp_executesql(#cm1)". If I tried to make it one command instead of two, it then says "ALTER VIEW must be the first statement in a query batch."
What can I do to get my cursor to work correctly?
EDIT: The following code is the working solution I found, thanks to IsItGreyOrGray (not sure if I'm supposed to edit it into my original post, or create a new one):
code for cursor
declare commands cursor for
SELECT REPLACE(DBNAME,DBNAME, 'USE ' + DBNAME),
REPLACE(REPLACE(REPLACE(DESCRIPTION,'OLDLINKEDSERVER', 'NEWLINKEDSERVER'),'CREATE VIEW', 'ALTER VIEW'), '''', '''''') AS CMD
FROM #TMP2
declarations
declare #cmd1 varchar(max)
declare #cmd2 varchar(max)
iterate through cursor
open commands
fetch next from commands into #cmd1, #cmd2
while ##FETCH_STATUS=0
begin
declare #cmd3 VARCHAR(MAX) = CONCAT(#cmd1, '; declare #cmd1 varchar(max) = ''', #cmd2, ''' exec(#cmd1)')
exec(#cmd3)
fetch next from commands into #cmd1, #cmd2
end
close commands
deallocate commands
Your issue is context switching.
Printing out the results will give you a working script that changes the databases correctly and makes the schema changes. But that's because it will all run in the context of your query window.
However, each EXEC runs in its own context.
If I run the following script, the output is "master". In #cmd1 I successfully switch to MSDB, but as soon as that command is complete, the context is lost and I'm back in MASTER. #Cmd2 then runs in its own context, which has not been switched from the database it was called from, so it runs in MASTER
USE master
GO
DECLARE #cmd1 VARCHAR(1000) = 'USE msdb'
DECLARE #cmd2 VARCHAR(1000) = 'SELECT DB_NAME()'
EXEC(#cmd1)
EXEC(#cmd2)
In order to make your cursor executions work, you'll need to combine #cmd1 with #cmd2 to make a single executable variable with something along the lines of
'USE [database1]; CREATE TABLE test (id INT)'
Since you're getting the query batch failures you might need to nest your executions...
This fails:
USE master
GO
DECLARE #cmd1 VARCHAR(1000) = 'USE msdb'
DECLARE #cmd2 VARCHAR(1000) = 'create procedure test as SELECT DB_NAME()'
DECLARE #cmd3 VARCHAR(MAX) = CONCAT(#cmd1, ' ', #cmd2)
EXEC (#cmd3)
This succeeds:
DECLARE #cmd1 VARCHAR(1000) = 'USE msdb'
DECLARE #cmd2 VARCHAR(1000) = 'create procedure test as SELECT DB_NAME()'
DECLARE #cmd3 VARCHAR(MAX) = CONCAT(#cmd1, '; declare #cmd1 varchar(max) = ''', #cmd2,''' exec(#cmd1)' )
EXEC (#cmd3)

Stored procedure to alter database recovery mode isnt compiling

I am trying to create a stored procedure that would be generic. I am trying to alter a database and set the recovery mode to either simple or full. It would accept database name and mode as parameter.
The SQL query executes in the context of the master database and alters the database specified. I am trying to incorporate it via Execute SQL task in SSIS. I need the stored procedure to reside in the database that is going to perform the operation on. Not sure how that is going to work. USE database keyword is not allowed in the stored procedure...
The original query works fine but I am facing an issue while trying to execute the stored procedure in the database.It says 'RECOVERY' is not a recognized SET option.
Original query:
use master
ALTER DATABASE XYZ
SET RECOVERY FULL
Stored procedure:
USE XYZ
GO
CREATE PROCEDURE DatabaseRecoveryMode
(#mode varchar(10),
#database varchar(50))
AS
BEGIN
ALTER DATABASE #database
SET RECOVERY #mode
END
The ALTER DATABASE documentation shows the recovery model is a keyword, not a variable. You'll need to construct and execute a dynamic SQL statement for this.
CREATE PROCEDURE dbo.DatabaseRecoveryMode
(
#mode nvarchar(11),
#database sysname
)
AS
IF #mode NOT IN(N'SIMPLE', N'BULK_LOGGED', N'FULL')
BEGIN
RAISERROR('Recovery model must be SIMPLE, BULK_LOGGED, OR FULL', 16, 1);
RETURN 1;
END;
DECLARE #SQL nvarchar(MAX) = N'ALTER DATABASE '
+ QUOTENAME(#database)
+ N' SET RECOVERY '+ #mode + N';';
EXECUTE(#SQL);
GO
You need to use dynamic SQL
USE XYZ
GO
Create Procedure DatabaseRecoveryMode
(
#mode varchar(10),
#database varchar(50)
)
AS
begin
DECLARE #SQL NVARCHAR(MAX)
DECLARE #db NVARCHAR(60), #Use NVARCHAR(100)
SET #db = N'master'
SET #Use = N'Use ' + #db
SET #SQL = #Use + N' ALTER DATABASE '+ #database + N' SET RECOVERY ' + #mode ;
--SELECT #SQL
EXEC sys.sp_executesql #SQL ;
end
GO

how to move excel file data to sql using bulk insert

i want to move all my data from excel files to tables in sql, dynamically.
I have used below procedure. but no luck. Plz help.
alter PROCEDURE SP_InsertTotables #tablename nvarchar(max),#filelocation nvarchar(max)
-- Add the parameters for the stored procedure here
AS
BEGIN
declare #sql nvarchar(max)
SET NOCOUNT ON;
Set #sql= 'BULK INSERT '+#tablename +' FROM '''+#filelocation+''' WITH (FIELDTERMINATOR = ''\t'',ROWTERMINATOR = ''\n'',FIRSTROW = 2)'
print #sql;
go

SQL Server Select-Into stored procedure

I am trying to write a custom stored procedure to carry out a select into operation. I want to copy a table (or some columns from a table) from one database to another. I am using SQL Server 2012
CREATE Procedure select_into
AS
Begin
#selection varchar(128),
#newtabname varchar(128)
#fromtabname varchar(128)
Select selection,
INTO table1,
FROM table2,
WHERE selection = #selection AND table1 = #newtabname AND table2 =#fromtabname;
go
EXEC select_into, Ecode, relational_db.dbo.work, dbo.Work_Data;
I get an error message indicating a syntax error near the "." in relational_db.dbo.work.
I would appreciate any help in getting this right
You have a missing comma in parameter list and wrong syntax for procedure declaration. It should be::
CREATE Procedure select_into
(
#selection varchar(128),
#newtabname varchar(128),
#fromtabname varchar(128)
)
AS
Begin
BUT, in addition your syntax for an INSERT INTO contains extra commas and you cannot perform dynamic T-SQL that way.
Can I suggest you first learn TSQL's syntax for SQL Server.
Try something like this ...
CREATE Procedure select_into
#selection NVARCHAR(128),
#newtabname NVARCHAR(128),
#fromtabname NVARCHAR(128)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql NVARCHAR(MAX);
SET #sql = N'Select ' + QUOTENAME(#selection) +
N' INTO ' + QUOTENAME(#newtabname) +
N' FROM ' + QUOTENAME(#fromtabname)
EXECUTE sp_executesql #sql
END

Resources