Executing stored procedure(with parameters) using sp_msforeachdb - sql

I'm going to get data from multiple databases (on same server) for this I came across a solution of using sp_msforeachdb. Now what I actually want to know about it is how can I pass stored procedure (with some parameters) to it. Here is the chunk of code I've tried on it but I'm in vein to pass parameters. Here my stored procedure has 3 parameters like BookCatId, IsAvailable and BookVersion. I have to pass these three parameters to usp_Get_AllBooks.
Is there any other efficient way to get records from multiple databases via single stored procedure? any suggestion will be highly appreciated.
Thank you.
EXECUTE sp_msforeachdb 'USE ?
IF DB_NAME() NOT IN(''master'',''msdb'',''tempdb'')
exec usp_Get_All_Books'

I have found a way to do this this might help someone, still I'm looking for a quick gentle solution. Any efficient solution will be is most welcome.
EXECUTE sp_msforeachdb 'USE ?
IF DB_NAME() NOT IN(''master'',''msdb'',''tempdb'')
exec usp_Get_All_Books #BookCatId = ''44'', #IsAvailable = ''1'',#BookVersion= ''2.2'''

Related

Stored Procedure Into Common Table Expression Workaround?

I need to essentially wrap a common table expression around the output from a stored procedure, obviously the stored procedure cannot be called directly from within the CTE so I am trying to find a workaround.
I have tried using SELECT FROM OPENROWSET, which initially looked like it solved the problem - however some of the stored procedures I need to call contain sp_executesql commands so it generates an error -
The metadata could not be determined because statement 'EXEC sp_executesql #SQL' in procedure 'sp_CustomerAndWorkers' contains dynamic SQL. Consider using the WITH RESULT SETS clause to explicitly describe the result set."
I have also looked at OPENQUERY, but that doesn't allow parameters to be included.
Is there any other method I could consider? Would be be possible
Thanks in advance.
Have you considered Temporary tables?
Example:
INSERT INTO #tempTable
EXEC sp_executesql

Executing Stored Proc in Pervasive Control Center

I am relatively new to Pervasive Control Center and I was wondering if I wanted to test a stored Procedure to see its results, how would I simply select that stored proc? I have:
Select SP_test_getMeasure06
I am sure I am missing something because I know this is legal my syntax must be off slightly.
Thanks in advance!
You can execute a stored procedure using either Call or Exec. For example:
exec SP_test_getMeasure06
You'll need to make sure your stored procedure uses the RETURNS clause to get data back. For more information check out the Stored Procedure docs.

List of stored procedure which using another stored procedure

We have almost 2100+ stored procedure in database. we are not able to check each stored procedure to find out whether it is using another stored procedure or not.
Any query to find out same?
Thanks,
Dhruval Shah
As long as you don't have dynamic SQL code, you can get the dependencies from SQL Server.
This article explains it pretty well.
You could use the free SQL Search tool from Red Gate

How do I execute sql text passed as an sp parameter?

I have a stored procedure with an nvarchar parameter. I expect callers to supply the text for a sql command when using this SP.
How do I execute the supplied sql command from within the SP?
Is this even possible?-
I thought it was possible using EXEC but the following:
EXEC #script
errors indicating it can't find a stored procedure by the given name. Since it's a script this is obviously accurate, but leads me to think it's not working as expected.
Use:
BEGIN
EXEC sp_executesql #nvarchar_parameter
END
...assuming the parameter is an entire SQL query. If not:
DECLARE #SQL NVARCHAR(4000)
SET #SQL = 'SELECT ...' + #nvarchar_parameter
BEGIN
EXEC sp_executesql #SQL
END
Be aware of SQL Injection attacks, and I highly recommend reading The curse and blessing of Dynamic SQL.
you can just exec #sqlStatement from within your sp. Though, its not the best thing to do because it opens you up to sql injection. You can see an example here
You use EXECUTE passing it the command as a string. Note this could open your system up to serious vulnerabilities given that it is difficult to verify the non-maliciousness of the SQL statements you are blindly executing.
How do I execute the supplied sql command from within the SP?
Very carefully. That code could do anything, including add or delete records, or even whole tables or databases.
To be safe about this, you need to create a separate user account that only has dbreader permissions on just a small set of allowed tables/views and use the EXECUTE AS command to limit the context to that user.

How do I run SQL queries on different databases dynamically?

I have a sql server stored procedure that I use to backup data from our database before doing an upgrade, and I'd really like it to be able to run the stored procedure on multiple databases by passing in the database name as a parameter. Is there an easy way to do this? The best I can figure is to dynamically build the sql in the stored procedure, but that feels like its the wrong way to do it.
build a procedure to back up the current database, whatever it is. Install this procedure on all databases that you want to backup.
Write another procedure that will launch the backups. This will depend on things that you have not mentioned, like if you have a table containing the names of each database to backup or something like that. Basically all you need to do is loop over the database names and build a string like:
SET #ProcessQueryString=
'EXEC '+DatabaseServer+'.'+DatabaseName+'.dbo.'+'BackupProcedureName param1, param2'
and then just:
EXEC (#ProcessQueryString)
to run it remotely.
There isn't any other way to do this. Dynamic SQL is the only way; if you've got strict controls over DB names and who's running it, then you're okay just truncating everything together, but if there's any doubt use QUOTENAME to escape the parameter safely:
CREATE PROCEDURE doStuff
#dbName NVARCHAR(50)
AS
DECLARE #sql NVARCHAR(1000)
SET #sql = 'SELECT stuff FROM ' + QUOTENAME(#dbName) + '..TableName WHERE stuff = otherstuff'
EXEC sp_ExecuteSQL (#sql)
Obviously, if there's anything more being passed through then you'll want to double-check any other input, and potentially use parameterised dynamic SQL, for example:
CREATE PROCEDURE doStuff
#dbName NVARCHAR(50)
#someValue NVARCHAR(10)
AS
DECLARE #sql NVARCHAR(1000)
SET #sql = 'SELECT stuff FROM ' + QUOTENAME(#dbName) + '..TableName WHERE stuff = #pOtherStuff'
EXEC sp_ExecuteSQL (#sql, '#pOtherStuff NVARCHAR(10)', #someValue)
This then makes sure that parameters for the dynamic SQL are passed through safely and the chances for injection attacks are reduced. It also improves the chances that the execution plan associated with the query will get reused.
personally, i just use a batch file and shell to sqlcmd for things like this. otherwise, building the sql in a stored proc (like you said) would work just fine. not sure why it would be "wrong" to do that.
best regards,
don
MSSQL has an OPENQUERY(dbname,statement) function where if the the server is linked, you specify it as the first parameter and it fires the statement against that server.
you could generate this openquery statement in a dynamic proc. and either it could fire the backup proc on each server, or you could execute the statement directly.
Do you use SSIS? If so you could try creating a couple ssis packages and try scheduling them,or executing them remotely.