Need to create a stored procedure - sql

if(filename==x)
exec stored procedure1
else if (filename==y)
exec stored procedure2
else
exec stored procedure3
filename is stored in a table .
How to do this in sql server?

Is there a part you're having trouble with? This is how you would get the file name from a table:
Declare #filename nvarchar(100)
Select #filename = filename_column from tablename
The syntax for if statements is
If #filename='xxx'
Begin
...
End
Executing a stored procedure is done with exec, just like in your pseudo code.

Related

How to know the last database before a procedure (in another database) was called

The caller of the procedure who is in database AAA:-
use AAA
exec BBB.dbo.ap_MyProc
The procedure being called which is in database BBB:-
use BBB
create procedure ap_MyProc as
print 'We want a way to return the database name AAA'
Any advice would be appreciated.
Well i can now answer my own question. We can exploit syslockinfo...
declare #db_name varchar(255)
select
#db_name = db_name(rsc_dbid)
from
master.dbo.syslockinfo
where
req_spid = ##SPID
and req_ownertype = 4 --"exSession"
and rsc_dbid <> db_id()
select #db_name = isnull(#db_name, db_name())
print #db_name
Works in SQL 2008 as well. :)
Can your stored procedure be edited? If yes, I think you can edit the stored procedures and add one more parameter for the database name.
And during calling the stored procedure, you should pass the db_name() into the new parameter, so that your stored procedure could know which database is calling it
DECLARE #ServerName varchar(50) = db_name()
EXEC [dbo].[SP] #newParams = #ServerName

Storing one or several sql statements as a variable in a stored procedure in SQL Server

Let's say I have a stored procedure. Is it possible to store multiple sql statements in a variable inside a stored procedure and later execute it when the procedure is invoked?
Example:
CREATE PROCEDURE dbo.StoredProcedure
(
#parameter1 int = QUERIES_HERE
)
AS
/*something like execute #parameter1*/
RETURN
Yes, like this:
DECLARE #SQLQuery varchar(500)
SET #SQLQuery = 'SELECT * FROM Employees WHERE EmployeeID = 123'
EXECUTE(#SQLQuery)

Issue in passing parameter on stored procedure

I have created a procedure without any problem. I have tested before creating the procedure but when I try to execute my procedure I have a problem only in the first parameter I have
SQL SERVER 2008 SHOW ME Error syntax in 'Mundus'
ProcAvancementTotalbyEtab 'Erasmus Mundus','C:\Users\AA\Desktop\Table1.xlsx'
and After I try to make like this:
ProcAvancementTotalbyEtab N'Erasmus Mundus','C:\Users\AA\Desktop\Table1.xlsx'
This is my stored procedure:
alter proc ProcAvancementbyEtab
(
#nameEtabb nvarchar(50),
#FilePath nvarchar(60)
)
as
begin
EXEC
(
'
SET NOCOUNT OFF;
insert into Avancement(nameEtab)
SELECT distinct T.[EFP]
From OPENROWSET(''MICROSOFT.ACE.OLEDB.12.0'',
''Excel 12.0;Database='+#FilePath+''',
''SELECT *
FROM [Sheet1$]'') T
where T.[EFP] is not null and T.[Avancement] is not null
and
not exists (select * from Avancement where
Avancement.nameEtab=T.[EFP])
and T.[EFP]='+#nameEtabb+'
')
end
When I comment this line
and T.[EFP]='+#nameEtabb+'
it works fine so my probleme is here
and T.[EFP]='+#nameEtabb+'
and when I test without stored procedure it work also OMG.
My guess is that name is stored as a string. You need an extra pair of quotes:
and T.[EFP] = '''+#nameEtabb+'''

Calling one stored procedure within another stored procedure using variables from first stored procedure

I have a stored procedure say #Create_Dummy1 which is being passed a variable. This is declared as #Dummy_Variable1 in this stored procedure.
Next I need to call another stored procedure #Create_Dummy2 from #Create_Dummy1. I need to pass #Dummy_Variable1 in the exec statement.
But if I try to do this the string #Dummy_Variable1 is only being passed instead of the value it holds.
I'm executing procedures inside other procedures like this:
DECLARE #childResult int, #loaErrorCode int, #loaErrorMessage varchar(255)
EXEC #childResult = [dbo].[proc_sub_getSomething] #schemes_id = #foo_schemes_i, #errorCode = #loaErrorCode OUTPUT , #errorMessage = #loaErrorMessage OUTPUT
Should it still not work you should edit your question to show your exact code.
This should work:
create procedure Create_Dummy1
(
#Dummy_Variable1 int
)
as
exec Create_Dummy2 #Dummy_Variable1
Go
And
create procedure Create_Dummy2
(
#Dummy_Variable1 int
)
as
Select * From yourTable WHERE tableColumn = #Dummy_Variable1
And this is how you call it:
exec Create_Dummy1 1
Hope this helps.

SQL select print out results of stored procedure

My businesses application supports only reporting with selected data from SQL server.In one business process I have very complicated stored procedure which using others stored procs and it was designed to print out results as log of job done. What I want to catch that print out and select it as varchar(max) so my app can handle that data and display to user.
Here is sample scenario described in TSQL code:
create procedure sp_test_print_out
as
begin
Print 'Test';
print 'Test 1';
end
go
create procedure sp_test_print_out_to_select
as
declare #printOut varchar(max)
set #printOut = exec sp_test_print_out --How I can achieve this ?
select #printOut
end
go
exec sp_test_print_out_to_select
You can try setting the values in output parameter
create procedure sp_test_print_out
#printMessages varchar(max) output
as
begin
set #printMessages='Test'
Print 'Test';
set #printMessages= #printMessages + CHAR(10)
set #printMessages= #printMessages + 'Test 1'
print 'Test 1';
end
go
create procedure sp_test_print_out_to_select
as
begin
declare #printOut varchar(max)
exec sp_test_print_out #printOut output -- can be achieved using output parameter ?
select #printOut
end
go
exec sp_test_print_out_to_select
There is also one rough and probably BAD way to get selected data from print commands inside stored procedure.
Command xp_cmdshell and sqlcmd can do the JOB. Xp_cmdshell is mostly disabled and not allowed to use at most of SQL servers because of security reasons.
Here is code:
CREATE TABLE #temp
(OUTPUT VARCHAR(MAX));
declare #cmd varchar(800);
set #cmd = 'sqlcmd -d RobotTest -Q "exec sp_test_print_out"';
INSERT INTO #TEMP
exec xp_cmdshell #cmd ;
select output from #temp;
drop table #temp;