Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 7th, 2011, 11:07 AM
Authorized User
Points: 169, Level: 3
Points: 169, Level: 3 Points: 169, Level: 3 Points: 169, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: Northampton, , United Kingdom.
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
Reply With Quote
  #2 (permalink)  
Old April 8th, 2011, 01:34 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
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.
Reply With Quote
The Following User Says Thank You to om_prakash For This Useful Post:
fullingdale (April 8th, 2011)
  #3 (permalink)  
Old April 8th, 2011, 04:14 AM
Authorized User
Points: 169, Level: 3
Points: 169, Level: 3 Points: 169, Level: 3 Points: 169, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: Northampton, , United Kingdom.
Posts: 35
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you for the info.
__________________
Regards,

Sean
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:09 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.