MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 23rd, 2004, 05:28 PM
monfu monfu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Dublin
Posts: 40 monfu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 25 sec
Reputation Power: 6
convert SQL code to Stored Proc

Dear All,

Lately i have started to convert my sql code to stored procedures, however i am still a bit new on this matter.

I have the following Sql Statement which I wish to convert to a stored procedure:-

SELECT MAX(" & id & ") As maxID FROM " & table

where id and table are parameters that I am passing to this sql from a function which is called getMaxID

for example, in the vb.net code i call it as follows:-

getMaxID("personID", "persons")

Can anyone tell me how to convert it to a Stored Proc please

Thanks for your help and time

Johann

Reply With Quote
  #2  
Old December 26th, 2004, 07:06 AM
elfy elfy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Poland
Posts: 11 elfy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 7 sec
Reputation Power: 0
MSSQL 2000
Code:
CREATE PROCEDURE [dbo].[GetMaxID] 

	@ID			VARCHAR(128),
	@TableName		VARCHAR(128)
AS

DECLARE @SQLString 	VARCHAR(8000)

SELECT @SQLString = 'SELECT MAX( ' + @ID + ' ) AS maxID FROM ' + @TableName

EXEC ( @SQLString )

GO


In MSSQL2000 column and table name maximum length is 128 chars.

There is few methods to call stored procedure in MSSQL. You can call above stored procedure for example this way:
Code:
exec GetMaxID 'personID', 'persons'

Reply With Quote
  #3  
Old December 26th, 2004, 09:10 AM
monfu monfu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Dublin
Posts: 40 monfu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 25 sec
Reputation Power: 6
it's not exactly what i was after, however i decided to use the @identity after doing an insert

Thanks however for your help

Johann

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > convert SQL code to Stored Proc


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway