Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2011, 11:07 AM
Authorized User
 
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default Function Question

Could someone offer a simple example of a function which takes an input parameter, such as a NAME and returns the appropriate ID for the value from a table:

ID NAME
1 Dave
2 Brian
3 Charles

But, where the NAME is not present, add it to the list and return the new ID

function "Dave" = 1
function "Brian" = 2
function "Robert" = 4 (and "Robert" added to the table)
__________________
Regards,

Sean
 
Old April 8th, 2011, 01:34 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

You will not be able to insert/alter the data in a function. Instead you need to create stored procedure which does this.
something like:
Code:
ALTER procedure dbo.getData (@username varchar(50), @userid int output)
AS
begin
select @userid = userid from users where userName = @username
if @userid is not null 
begin
insert into users (username) values (@username)
SELECT @userid = @@IDENTITY 
end
end
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
The Following User Says Thank You to om_prakash For This Useful Post:
fullingdale (April 8th, 2011)
 
Old April 8th, 2011, 04:14 AM
Authorized User
 
Join Date: Apr 2008
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you for the info.
__________________
Regards,

Sean





Similar Threads
Thread Thread Starter Forum Replies Last Post
Another Document ( ) Function Question bonekrusher XSLT 2 August 4th, 2006 10:45 AM
FormatCurrency Function Question kwilliams VBScript 0 May 12th, 2006 09:17 AM
Question on the ARRAY function savoym VBScript 6 May 31st, 2005 06:53 AM
Function Question stu9820 ASP.NET 1.0 and 1.1 Professional 9 December 4th, 2003 12:29 PM
Shell Function Question SerranoG Access VBA 4 November 12th, 2003 07:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.