How to deploy no of stored procedures in database using SSIS task? - ssis-2012

How to i deploy(run) all my stored procedures in database using ssis tasks.For example
I have around 800 newly created stored procedures stored in separate folder.I want to run(F5)all these store procedures from my folder.Not required to execute the procedure.
It will take much time If I open each stored procedure manually and Press F5 Button in IDE:-).
Is there any automation process to achieve this in SSIS ?

Why SSIS? If you are not specific about SSIS then you can run all of the stored procedure at once using DOS command like below
(taken from This Post)
for %%f in (*.sql) do sqlcmd -U<User> -P<Pass> -S<Server> -d<Database> -i%%f
(OR)
You can write a script file (*.sql) and include call to each procedure like
use [DBNAME]
GO
exec proc1
exec proc2
...
exec procn
Then run it thorugh SQLCMD command line utility like
sqlcmd -U<User> -P<Pass> -S<Server> -i <script file name>
As well take a look at this post
Way of executing several .sql files at once in SQL Server

Related

SQL Server Agent Job called a Stored Procedure that used Linked Server did not work

I have a stored procedure that uses linked server:
The linked server was created pointing to the Source Database server and Security is "Be made using the login's current security context"
The credential was created using an AD service account
The proxy was created with a check on the "Operating system (CmdExec) "
The stored procedure used the linked server bame in the insert/select command. The stored procedure worked well when I executed it within SSMS.
However when I run the stored procedure from SQL Server Agent Job step, using SQLCMD, it did not work.
Here is the example of my stored procedure:
USE [MyDatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[TEST_LINKED_SERVER]
AS
BEGIN
INSERT INTO MyDatabaseName.dbo.MyTableName
SELECT *
FROM [linkedservername].SourceDatabaseName.dbo.SourceTableName
END
GO
After creating the stored procedure, I executed it from SSMS - stored procedure. I worked well.
MyTableName had data inserted from the SourceTableName.
I then created a SQL Server Agent job with two steps:
Step #1: Run a stored procedure, truncate data from MytableName
Step #2: Run the "TEST_LINKED_SERVER" stored procedure, update data to Mytablename, by insert data from the linked server/database/table on another server using the linked server.
Step #2 was setup as follows:
Type: Operating system (CmdExec)
Run as: ProxyName
Command: SQLCMD -Q "EXEC dbo.TEST_LINKED_SERVER"
The job completed successfully. However MytableName did not have any data. It is empty. Data was not inserted to MyTableName.
Would you have any idea? Do I need to update my SQLCMD? And how?
Your help is much appreciated.
I found the solution and it worked:
SQLCMD -E -S MyServerName -D MyDatabaseName -Q "EXEC dbo.TEST_LINKED_SERVER"

I need to restore db from dump and cannot do it

I have a database dump at D:/backup.dump. I try to restore my database min_ro: I open psql.exe plugin. There are words
min_ro=#
Then I write restore command:
min_ro=# psql min_ro < D:/backup.dump
Then happens nothing. My database is not restored. What is wrong? It's first time using psql.
Update. I don't need psql only - I need to restore db from dump and cannot do it.
psql is not a SQL statement, so it doesn't make sense to enter that at the psql prompt which is there to run SQL statements (or psql meta commands).
c:\> psql min_ro < D:/backup.dump
needs to be entered on the (Windows) command line, not inside psql.
You can however just run the SQL script (which I assume your dump is) by using the \i ("include") meta command in `psql``
c:\> psql min_ro
min_ro=# \i D:/backup.dump
When you restore your database at pgAdminIII (by right-click at database name then choice 'restore') you can't see .dump files at backup list by default. That was my mistake forced me to try another ways to restore DB from dump.
But if you simply change file types to 'All files' you can restore your database from dump as usially.

Why can't I execute msdb.dbo.send_dbmail from a stored procedure being executed in a job?

I have a job scheduled to run on my server. That job executes a stored procedure. Inside that stored procedure I am executing msdb.dbo.sp_send_dbmail. When I execute the stored procedure logged in as an admin it runs fine. When the job runs though, it fails with the following error:
Executed as user: AD\sql_server. Failed to initialize sqlcmd library with error number -2147467259. [SQLSTATE 42000](Error 22050). The step failed.
I have tried modifying the stored procedure and adding in WITH EXECUTE AS OWNER. When I do this the stored procedure fails with the following error:
Executed as user: AD\sql_server. The EXECUTE permission was denied on the object 'sp_send_dbmail', database 'msdb', schema 'dbo'. [SQLSTATE 42000](Error 229). The step failed.
What do I need to do to be able to execute a stored procedure in a job that executes msdb.dbo.sp_send_dbmail?
unfortunately WITH EXECUTE AS OWNER is not going to solve your problem.
You may have to add the user as a part of the built in database mail role with something like:
USE msdb;
EXEC sp_addrolemember 'DatabaseMailUserRole', 'AD\sql_server'
Check out this post.

How to run stored procedure if it's in a separate schema

I am using SQL Server 2008 R2, and I have created a schema Test and in that schema, I created a stored procedure.
I wanted to run it in the text mode by issuing this query:
EXEC SP_HELPTEXT SCHEMA.SPROC
But while running the above query I get this error:
Incorrect syntax near '.'.
Can someone please help me here to resolve this issue.
Try this
EXEC SP_HELPTEXT 'SCHEMA.SPROC'
sp_helptext to display the result when sp's or any other tsql binded scripts run without schema, for example:
sp_helptext <nameof sp>
but when you want to run it with schema name as initial then run it into single quotes
sp_helptext 'schma.<nameofsp>

How to write stored procedures to separate files with mysqldump?

The mysqldump option --tab=path writes the creation script of each table in a separate file. But I can't find the stored procedures, except in the screen dump.
I need to have the stored procedures also in separate files.
The current solution I am working on is to split the screen dump programatically. Is there a easier way?
The code I am using so far is:
#save all routines to a single file
mysqldump -p$PASSWORD --routines --skip-dump-date --no-create-info --no-data --skip-opt $DATABASE > $BACKUP_PATH/$DATABASE.sql
#save each table to its file
mysqldump -p$PASSWORD --tab=$BACKUP_PATH --skip-dump-date --no-data --skip-opt $DATABASE
Even if I add --routines to the second command, they will not get their own files.
I created a script to output to a separate file.
https://gist.github.com/temmings/c6599ff6a04738185596
example: mysqldump ${DATABASE} --routines --no-create-info --no-data --no-create-db --compact | ./seperate.pl
File is output to the directory(out/).
$ tree
.
└── out
├── FUNCTION.EXAMPLE_FUNCTION.sql
└── PROCEDURE.EXAMPLE_PROCEDURE.sql
The mysqldump command does not support dumping stored procedures into individual files.
But, it is possible to do it using the mysql command.
mysql --skip-column-names --raw mydatabase -e "SELECT CONCAT('CREATE PROCEDURE `', specific_name, '`(', param_list, ') AS ') AS `stmt`, body_utf8 FROM `mysql`.`proc` WHERE `db` = 'mydatabase' AND specific_name = 'myprocedure';" 1> myprocedure.sql
For a more complete example, using Windows Batch, look into my answer on another question.
MySQL - mysqldump --routines to only export 1 stored procedure (by name) and not every routine
I think the answer is: it is not possible without post-processing
This writes table definitions (not SPs) fwiw:
mysqldump -u<username> -p<password> -T<destination-directory> --lock-tables=0 <database>
One snag I ran into was, make sure you put enough permissions on . I just did chmod 777 on it.
A note on this--MySQL will write out the table structures in .sql files, and the data in .txt files. I wish it would just do it normal, thanks.

Resources