August 27, 2018 at 6:30 am
Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?
August 27, 2018 at 6:55 am
Jackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?
No problem at all ... though this is not necessarily good programming practice.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
August 27, 2018 at 7:00 am
Jackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?
Yes, it is.
But I would not recommend to do it.
Create different procedures for different tasks.
But - you can assign the same name "with index" to those different procedures.CREATE PROCEDURE TestSelect;1
as
select * from sys.objects
go
CREATE PROCEDURE TestSelect;2
as
select * from sys.columns
go
exec TestSelect;1
exec TestSelect;2
When you drop procedure TestSelect you drop every instance of it.
_____________
Code for TallyGenerator
August 27, 2018 at 7:01 am
Can you provide a rough example or a link? Also, why is it not good practice?
August 27, 2018 at 7:03 am
Sergiy - Monday, August 27, 2018 7:00 AMJackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?Yes, it is.
But I would not recommend to do it.
Create different procedures for different tasks.But - you can assign the same name "with index" to those different procedures.
CREATE PROCEDURE TestSelect;1
asselect * from sys.objects
goCREATE PROCEDURE TestSelect;2
asselect * from sys.columns
goexec TestSelect;1
exec TestSelect;2When you drop procedure TestSelect you drop every instance of it.
Can i still pass parameters to the SP that way?
August 27, 2018 at 7:07 am
I think i understand. The SP is for Inventory lot allocation. So, I should just create separate procedures. i.e. IM_Lot_Select, IM_Lot_Update, IM_Lot_Delete?
August 27, 2018 at 7:07 am
Sergiy - Monday, August 27, 2018 7:00 AMJackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?Yes, it is.
But I would not recommend to do it.
Create different procedures for different tasks.But - you can assign the same name "with index" to those different procedures.
CREATE PROCEDURE TestSelect;1
asselect * from sys.objects
goCREATE PROCEDURE TestSelect;2
asselect * from sys.columns
goexec TestSelect;1
exec TestSelect;2When you drop procedure TestSelect you drop every instance of it.
Or you could just have the flag as a parameter to the stored procedureCREATE PROCEDURE TestSelect
(
@Flag int
) as
IF @Flag=1
select * from sys.objects
ELSE IF @Flag=2
select * from sys.columns
go
exec TestSelect 1
exec TestSelect 2
August 27, 2018 at 7:07 am
Jackie Lowery - Monday, August 27, 2018 7:03 AMSergiy - Monday, August 27, 2018 7:00 AMJackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?Yes, it is.
But I would not recommend to do it.
Create different procedures for different tasks.But - you can assign the same name "with index" to those different procedures.
CREATE PROCEDURE TestSelect;1
asselect * from sys.objects
goCREATE PROCEDURE TestSelect;2
asselect * from sys.columns
goexec TestSelect;1
exec TestSelect;2When you drop procedure TestSelect you drop every instance of it.
Can i still pass parameters to the SP that way?
It's easy to find out:CREATE PROCEDURE TestSelect;3
@NameLike nvarchar(20)
as
select * from sys.objects
where name like @NameLike
go
CREATE PROCEDURE TestSelect;4
@NameLike nvarchar(20)
as
select * from sys.columns
where name like @NameLike
go
exec TestSelect;3 '%s'
exec TestSelect;4 '%d'
_____________
Code for TallyGenerator
August 27, 2018 at 7:12 am
Sergiy - Monday, August 27, 2018 7:07 AMIt's easy to find out:
lol. Yes, that was a lazy question. Thanks.
August 27, 2018 at 7:21 am
Jonathan AC Roberts - Monday, August 27, 2018 7:07 AMSergiy - Monday, August 27, 2018 7:00 AMJackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?Yes, it is.
But I would not recommend to do it.
Create different procedures for different tasks.But - you can assign the same name "with index" to those different procedures.
CREATE PROCEDURE TestSelect;1
asselect * from sys.objects
goCREATE PROCEDURE TestSelect;2
asselect * from sys.columns
goexec TestSelect;1
exec TestSelect;2When you drop procedure TestSelect you drop every instance of it.
Or you could just have the flag as a parameter to the stored procedure
CREATE PROCEDURE TestSelect
(
@Flag int
) as
IF @Flag=1
select * from sys.objects
ELSE IF @Flag=2
select * from sys.columns
goexec TestSelect 1
exec TestSelect 2
The problem with this approach is it is a performance timebomb. The execution plan can get cached for one execution path and then need to run with the other one resulting in really terrible performance. Gail has a great blog post on this topic. https://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 27, 2018 at 7:27 am
I have read about that in the past, but didn't think about it. Another good point. I'm just going to split up the SPs based on function. Thanks guys.
August 27, 2018 at 10:00 am
Mode is quite OK to do as long as you make the initial proc just a driver proc: it adjusts param(s) as needed and then calls another, customized proc to do the processing required. In fact, this is actually quite useful when there are a lot of options and you want to be able to easily add more in the future.
CREATE PROCEDURE categ_driver
@param_value1 ...,
@param-value2 ..,.
@mode varchar(10)
AS
SET ...
IF @param ...
SET @param ...
IF @mode = 'AUDIT'
EXEC categ_audit_... @param ...
ELSE
IF @mode = 'REPORT'
EXEC categ_report_... @param ...
ELSE
IF @mode = 'SELECT'
EXEC categ_select_... @param ...
ELSE
IF @mode = 'UPDATE'
EXEC categ_update_... @param ...
...
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
August 27, 2018 at 6:59 pm
Jonathan AC Roberts - Monday, August 27, 2018 7:07 AMSergiy - Monday, August 27, 2018 7:00 AMJackie Lowery - Monday, August 27, 2018 6:30 AMIs it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?Yes, it is.
But I would not recommend to do it.
Create different procedures for different tasks.But - you can assign the same name "with index" to those different procedures.
CREATE PROCEDURE TestSelect;1
asselect * from sys.objects
goCREATE PROCEDURE TestSelect;2
asselect * from sys.columns
goexec TestSelect;1
exec TestSelect;2When you drop procedure TestSelect you drop every instance of it.
Or you could just have the flag as a parameter to the stored procedure
CREATE PROCEDURE TestSelect
(
@Flag int
) as
IF @Flag=1
select * from sys.objects
ELSE IF @Flag=2
select * from sys.columns
goexec TestSelect 1
exec TestSelect 2
The problem with such a method is that SQL Server won't necessarily compile the execution plan for what you want.. You can get some pretty serious less-than-optimal performance from such a thing.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply