Showing posts with label equivalent. Show all posts
Showing posts with label equivalent. Show all posts

Friday, February 24, 2012

avoid repeating "if...end" in a stored procedure

I would like to avoid repeating 4 time the procedure if end and instead use someting like "switch case", but I can not find the equivalent.
Is there any command that could do that?

thanks

[code]
CREATE PROCEDURE sa_default

@.etriduser int= null,
@.locator int = null,
@.choix int = null,
@.login varchar(50) = null

AS
if @.choix=0
begin
........
end

if @.choix=1
begin
.........
end

if @.choix=2
begin
.........
end

if @.choix=3
begin
......
end

RETURN
GO

[/code]What is it you are trying to accomplish in your condition?|||I am selection a list of project which change depending on the level of right of the user


CREATE PROCEDURE sa_default

@.etriduser int= null,
@.locator int = null,
@.choix int = null,
@.login varchar(50) = null

AS
if @.choix=0
begin
SELECT Tproject.idproject, Tproject.provider, Tproject.pnumber, Tproject.pdescription, Tproject.service, Tproject.location, Tproject.ponumber, Tproject.state, Tcompany.company, Tproject.datestart,
ROUND(CONVERT(float, CONVERT(float, (SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC <> 0 )) /
(SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC = 0)), 5) * 100 AS Expr1
FROM Tproject INNER JOIN Tcompanyproject ON Tproject.idproject = Tcompanyproject.etridproject INNER JOIN Tcompany ON Tcompanyproject.etridcompany = Tcompany.idcompany
WHERE (Tcompanyproject.type = 2) AND (Tproject.state IN (1, 2, 3)) and Tproject.locator=isnull(@.locator,locator) ORDER BY idproject
end
if @.choix=1
begin
SELECT DISTINCT Tproject.idproject, Tproject.provider, Tproject.pnumber, Tproject.pdescription, Tproject.service,Tproject.location, Tproject.ponumber, Tproject.state,
Tcompany.company, Tproject.datestart, ROUND(CONVERT(float, CONVERT(float, (SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC <> 0)) /
(SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC = 0)), 5) * 100 AS Expr1
FROM Tproject INNER JOIN Tcompanyproject ON Tproject.idproject = Tcompanyproject.etridproject
INNER JOIN Tcompany ON Tcompanyproject.etridcompany = Tcompany.idcompany
INNER JOIN Tuserproject ON Tproject.idproject = Tuserproject.etridproject
WHERE (Tcompanyproject.type = 2) AND (Tproject.state IN (1, 2, 3)) and (Tuserproject.etridperson=@.etriduser or Tproject.locator=@.locator) ORDER BY idproject
end
if @.choix=2
begin
SELECT DISTINCT Tproject.idproject, Tproject.provider, Tproject.pnumber, Tproject.pdescription, Tproject.service,Tproject.location, Tproject.ponumber, Tproject.state,
Tcompany.company, Tproject.datestart, ROUND(CONVERT(float, CONVERT(float, (SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC <> 0)) /
(SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC = 0)), 5) * 100 AS Expr1,Tuserproject.mc, Tuserproject.type, Tuserproject.wechart
FROM Tproject INNER JOIN Tcompanyproject ON Tproject.idproject = Tcompanyproject.etridproject
INNER JOIN Tcompany ON Tcompanyproject.etridcompany = Tcompany.idcompany INNER JOIN Tuserproject ON Tproject.idproject = Tuserproject.etridproject
WHERE (Tcompanyproject.type = 2) AND (Tproject.state IN (1, 2, 3)) and Tuserproject.etridperson=@.etriduser ORDER BY idproject
end

if @.choix=3
begin
SELECT Tproject.idproject, Tproject.provider, Tproject.pnumber, Tproject.pdescription, Tproject.service, Tproject.location, Tproject.ponumber, Tproject.state, Tcompany.company, Tproject.datestart,
ROUND(CONVERT(float, CONVERT(float, (SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC <> 0)) /
(SELECT SUM(NB) FROM TEChart WHERE Project = tproject.idproject AND QC = 0)), 5) * 100 AS Expr1, Tuserproject.mc, Tuserproject.type, Tuserproject.wechart
FROM Tproject INNER JOIN Tcompanyproject ON Tproject.idproject = Tcompanyproject.etridproject
INNER JOIN Tcompany ON Tcompanyproject.etridcompany = Tcompany.idcompany INNER JOIN Tuserproject ON Tproject.idproject = Tuserproject.etridproject
WHERE (Tcompanyproject.type = 2) AND (Tproject.state IN (1, 2, 3)) AND
(Tuserproject.etridperson IN (SELECT idperson FROM Tperson WHERE login LIKE @.login))
ORDER BY Tproject.idproject
end

RETURN
GO

|||You can use a CASE statement in some situations, but it seems like you want to execute code within your BEGIN... ENDs. It won't work in those cases. I don't believe there is anything like Javascript's switch you can use to execute code like that. Depending on what you're trying to do, dynamic SQL might be an answer. Really though, more information is needed on what will happen based on each IF to really give a good answer.|||A CASE statement may work when you just need to change the WHERE clause, but it looks like you change the query as well. It looks like IF times 4 is the way to go.|||I second trying the CASE, but if you can't make it work then I'd make turn those inner batches into their own procs you'll prb' get better performance cause you won't suffer from the selective query plan issues of using IFs.|||Thanks for your comments.

Can you tell me more about your idea about "turn those inner batches into their own procs "
I never hear about that, do you have any sample or link for documentation?

Thanks|||There's no great mystery to it. Problem 1: One of the publicised problems with stored procedure plans is they can get "confused" by IF statements. I believe the problem is because it originally stores only one plan that represents only one path through the code. So the other IF conditions may not be ready to run. I've not tested it this myself but sounds plausable.

Problem 2: The more complicated the plan, the more chance the query optimiser has of making the wrong choices. I've certainly seen this. The more conditions in the proc seems to increase the likelyhood of the query plan having performance issues. Not suprising really because there is a slider between compiling optimal code and compiling the code quickly. So the longer it takes to compile the code the more likely the optimiser will throw its hands in the air.

Both of these problems can be helped by converting some of the query into other stored procedures. These have their own plans so the optimiser can, typically, do a better job.|||I agree with that post 100% and it's true that 4 significantly different queries will have a very good chance of not being run optimally. Create 4 procs and within your application code, decide which one to call based on what that parameter is at run time. The 4 procs will run more efficiently.

Friday, February 10, 2012

Autonumber equivalent in SQL Server?

Hi

So far, I have only used Access which has an autonumber data type so that in some of my tables the id is automatically generated.
I guess this is a simple question but is there an equivalent data type in sql server?

Thanks in advance.Yes - Int with an Identity property set to Yes

autonomous transactions

Is there any equivalent to Oracle's autonomous transaction in sqlserver.

Depends on what does "autonomous transaction" mean.

|||No. There is no way to do this in SQL Server right now. You can write extended SPs or CLR SPs (SQL 2005) that performs a different connection to the database. But this approach has it's own limitations and scalability problems. One workaround that will work in SQL2000/2005 depending on your requirements is to use a table variable within a transaction to dump the information. Table variables do not participate in user transactions so you can later retrieve the rows and dump them into an error log table for example after the transaction has been rolled back.|||Thanks Umachander for the information.

Autonomous Transaction can be used for controlling transactions in

stored procedures. Autonomous Transactions allow you to create a new

subtransaction that may commit or rollback changes independent

of the parent transaction. Assume two procedures - Parent and child

procedure. The parent procedure calls child procedure. You can set the

statements with in the child procedure as Autonomous and this will be

independent of the transaction of the Parent procedure. Hope the above

explains the Autonomous Transaction.|||Helped me. Thanks to both of you I learned something today!|||

Hi Umachander,

I have read your comments and I had the same problem when using SSMA to convert a PL/SQL stored procedure to T-SQL.

Interesting note is the error message - "ERROR ORA2MS-2046 line: 3 col: 4: Conversion of PRAGMA AUTONOMOUS_TRANSACTION is not implemented in this version"

I sincerely believe that if SQL Server is to be RDBMS of choice, code from any other RDBMS must be convertable

If you have any suggestions please let me know as it would be most appreciated

|||The only workarounds are those I mentioned in my previous reply. And we are considering this feature for a future version of SQL Server.

autonomous transactions

Is there any equivalent to Oracle's autonomous transaction in sqlserver.

Depends on what does "autonomous transaction" mean.

|||No. There is no way to do this in SQL Server right now. You can write extended SPs or CLR SPs (SQL 2005) that performs a different connection to the database. But this approach has it's own limitations and scalability problems. One workaround that will work in SQL2000/2005 depending on your requirements is to use a table variable within a transaction to dump the information. Table variables do not participate in user transactions so you can later retrieve the rows and dump them into an error log table for example after the transaction has been rolled back.|||Thanks Umachander for the information.
Autonomous Transaction can be used for controlling transactions in stored procedures. Autonomous Transactions allow you to create a new subtransaction that may commit or rollback changes independent of the parent transaction. Assume two procedures - Parent and child procedure. The parent procedure calls child procedure. You can set the statements with in the child procedure as Autonomous and this will be independent of the transaction of the Parent procedure. Hope the above explains the Autonomous Transaction.
|||Helped me. Thanks to both of you I learned something today!|||

Hi Umachander,

I have read your comments and I had the same problem when using SSMA to convert a PL/SQL stored procedure to T-SQL.

Interesting note is the error message - "ERROR ORA2MS-2046 line: 3 col: 4: Conversion of PRAGMA AUTONOMOUS_TRANSACTION is not implemented in this version"

I sincerely believe that if SQL Server is to be RDBMS of choice, code from any other RDBMS must be convertable

If you have any suggestions please let me know as it would be most appreciated

|||The only workarounds are those I mentioned in my previous reply. And we are considering this feature for a future version of SQL Server.