all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

Can you simplfy my SP : I will have 97 IF/ELSE statements


Can you simplfy my SP : I will have 97 IF/ELSE statements Russell Mangel
2/13/2005 7:50:23 PM
sql server programming:
I am working with an old legacy SQL 7.0 database (it has poor design), and I
have to create a SP which does the following:

Determine the largest value of an 'int' column for a table.

Here is a working solution, but it will be very long, since I need to
support 97 tables, in my legacy database.

Is there an shorter/simpler way to achive my goal?

-- NorthWind database
CREATE PROCEDURE Utils_GetMaxID
@tableName varchar(32), -- a table name in Northwind
@maxID int OUTPUT -- return the largest PrimaryKey 'int' value
AS

IF @tableName = 'Employees'
SELECT @maxID = MAX(EmployeeID) FROM Employees
ELSE IF @tableName = 'Orders'
SELECT @maxID = MAX(OrderID) FROM Orders
ELSE
RAISERROR ('Table does not exist.', 16, 1)


-- Maybe we could use a temp table and dynamic SQL to get maxID?
-- I wish I could use, FROM @tableName (but SQL does not allow it)




Re: Can you simplfy my SP : I will have 97 IF/ELSE statements David Gugick
2/13/2005 11:40:36 PM
You can use dynamic SQL, but the connected user will require SELECT
access to the table:

CREATE PROCEDURE Utils_GetMaxID
@tableName varchar(32), -- a table name in Northwind
@columnName varchar(128), -- the column in the table
@maxID int OUTPUT -- return the largest PrimaryKey 'int' value
AS
Declare @nvcSQL nvarchar(4000)

Set @nvcSql = N'SELECT @maxID = MAX(' + @columnName + N') FROM [' +
@tableName + N']'
Exec sp_executesql @nvcSQL, N'@maxID INT OUTPUT', @maxID OUTPUT

--
David Gugick
Imceda Software
www.imceda.com
Re: Can you simplfy my SP : I will have 97 IF/ELSE statements --CELKO--
2/14/2005 6:58:15 AM
Why not just get rid of this poorly defined and coded module? It has
no cohesion at all -- what would you properly name it? The "find
_max_squid_or_employee_or_bankaccount_or.. or_apples_id" procedure!

Passing table names is absurd as well as a violation of the basic
prinicples of software engineering. Since each table is a totally
different kind of thing, their identifiers will be totally different.
You can get a SSN, a phone number or an ISBN back from this disaster.

But if you are determiened to keep doing it wrong, use dynamic SQL and
expect to get SQL injections, loss of integrity and loss of security.

Then find the guy that wrote this and kill him to prevent future
problems :) Then completely re-design this schema. The number of
tables sounds too high for a medium sized business and if he writes
code like this, he probably screwed up everything from the start.
Re: Can you simplfy my SP : I will have 97 IF/ELSE statements Vinod Kumar
2/14/2005 11:47:32 AM
Using Output Parameters with sp_executesql
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q262499

--
HTH,
Vinod Kumar
MCSE, DBA, MCAD, MCSD
http://www.extremeexperts.com

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

[quoted text, click to view]

Re: Can you simplfy my SP : I will have 97 IF/ELSE statements Russell Mangel
2/17/2005 5:48:43 PM
Thanks, this helped solve my problem.

I am converting data from a poorly designed database to a new database which
has much better schema.
So I won't be using this in the new database.

Russell Mangel
Las Vegas,


Re: Can you simplfy my SP : I will have 97 IF/ELSE statements Russell Mangel
2/17/2005 5:58:24 PM
I agree with your comments, exactly.

Remember, I stated that I was working with a "Legacy" database which was
originally designed (actually hacked is a better word), in 1996.

You are correct in assuming that the guy screwed everything up.
In fact I the database and client software he created, is the most
complicated fucked up system I have ever seen.
He had no plan, he just coded his way around everything. The source code
(VB6), is a mess, and unfix-able.

We are converting data from the old database, into the new database, and so
this is why I asked for help with the unusual query.

Unfortunately, no one killed him, he was only fired, and so he is currently
looking for a job working at your company!

Russell Mangel
Las Vegas, NV

[quoted text, click to view]

AddThis Social Bookmark Button