| Author |
Topic |
|
jonnod123
Starting Member
6 Posts |
Posted - 2008-04-28 : 10:18:54
|
| Hi All,I've been going around in circles and was wondering if anyone could help with this insert query. (I'm using SQL Server 2005).I have two tables aspnet_Users and aspnet_Draws. They have a 1 to many relationship, with UserID being the PK in aspnet_Users and the FK in aspnet_Draws. Table structure is:aspnet_DrawsID (PK)UserIdDrawDateNumberOfEntriesaspnet_UsersUserId (PK)UserNameI want to insert a row into aspnet_Draws. UserID needs to be taken from aspnet_Users where UserName is a particular value, e.g. NameXXXDrawDate and NumberOfEntries do not allow nulls and also need to be updated, though these values will be taken from a webForm and not from aspnet_Users. ID is automatically incremented.Any help or advice would be greatly appreciated.Cheers,Jon |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-28 : 10:26:17
|
You need to write front end application code to get DrawDate,NumberOfEntries & UserName from webform.Then call a stored procedure from application passing these values.It will be of form:-CREATE PROC SaveWebValues@NumberOfEntries int,--from form@DrawDate datetime = GETDATE()--date value from form@UserName = SUSER_SNAME()--name of user who is logged inASINSERT INTO aspnet_Draws (UserId,DrawDate,NumberOfEntries)SELECT UserId,@DrawDate,@NumberOfEntriesFROM aspnet_UsersWHERE UserName=@UserNameGO |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 10:28:43
|
[code]CREATE PROCEDURE dbo.spTest( @UserName VARCHAR(100))ASSET NOCOUNT ONUPDATE dSET d.DrawDate = GETDATE(), d.NumberOfEntries = d.NumberOfEntries + 1FROM aspnet_Draws AS dINNER JOIN aspnet_Users AS u ON u.UserID = d.UserIDWHERE u.UserName = @UserNameIF @@ROWCOUNT > 0 RETURNIF NOT EXISTS (SELECT * FROM aspnet_Users WHERE UserName = @UserName) INSERT aspnet_Users ( UserName ) VALUES ( @UserName )INSERT aspnet_Draws ( UserID, DrawDate, NumberOfEntries )SELECT UserID, GETDATE(), 1FROM aspnet_UsersWHERE UserName = @UserName[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonnod123
Starting Member
6 Posts |
Posted - 2008-04-29 : 05:44:02
|
| Hi Guys and thanks for your help.I'm getting a syntax error now. My stored procedure is:CREATE PROCEDURE [dbo].[aspnet_Draws_CreateDraws] -- Add the parameters for the stored procedure here @NumberOfEntries int, --from form @DrawDate datetime, --date value from form @UserName nvarchar(256), --name of user from formASINSERT INTO aspnet_Draws (UserId,DrawDate,NumberOfEntries)SELECT UserId,@DrawDate,@NumberOfEntriesFROM aspnet_UsersINNER JOIN aspnet_Users ON aspnet_Draws.UserId = aspnet_Users.UserId --INNER JOIN aspnet_Users ON aspnet_Users.UserId = aspnet_Draws.UserIdWHERE aspnet_Users.UserName = @UserNameGOAnd the error message is:Msg 156, Level 15, State 1, Procedure aspnet_Draws_CreateDraws, Line 11Incorrect syntax near the keyword 'AS'.Thanks in advance for any help.Jon |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-29 : 05:49:18
|
Change the INNER JOIN table to "aspnet_Draws".The code you posted is not the same I suggested. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonnod123
Starting Member
6 Posts |
Posted - 2008-04-29 : 06:39:18
|
| Hi Peso, Thanks for taking the time to look at this.I'm happy to use your code and it does compile. However, the values DrawDate and NumberOfEntries cannot be created dynamically, they need to be taken as user input from the webform. Also, the SP is not used to update existing records in aspnet_Draws, each input should be inserted as a new record in the table.This is my first attempt at a stored procedure, so sorry if this wasn't clear.Thanks,Jon |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-29 : 06:43:02
|
quote: Originally posted by jonnod123 Hi Peso, Thanks for taking the time to look at this.I'm happy to use your code and it does compile. However, the values DrawDate and NumberOfEntries cannot be created dynamically, they need to be taken as user input from the webform. Also, the SP is not used to update existing records in aspnet_Draws, each input should be inserted as a new record in the table.This is my first attempt at a stored procedure, so sorry if this wasn't clear.Thanks,Jon
thats what you are doing by passing the value in parameters. You jsut want your application to grab these values from webform and apss them onto stored procedure |
 |
|
|
jonnod123
Starting Member
6 Posts |
Posted - 2008-04-29 : 06:57:07
|
| I realise that Visakh, and it's what I've been trying to do (see post 3). But I'm getting a syntax error. |
 |
|
|
jonnod123
Starting Member
6 Posts |
Posted - 2008-04-29 : 06:58:58
|
| Post 4 sorry! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-29 : 07:07:01
|
quote: Originally posted by jonnod123 I realise that Visakh, and it's what I've been trying to do (see post 3). But I'm getting a syntax error.
there was an unwanted ,. remove it and tryCREATE PROCEDURE [dbo].[aspnet_Draws_CreateDraws]-- Add the parameters for the stored procedure here@NumberOfEntries int, --from form@DrawDate datetime, --date value from form@UserName nvarchar(256), --name of user from formASINSERT INTO aspnet_Draws (UserId,DrawDate,NumberOfEntries)SELECT UserId,@DrawDate,@NumberOfEntriesFROM aspnet_UsersINNER JOIN aspnet_Users ON aspnet_Draws.UserId = aspnet_Users.UserId --INNER JOIN aspnet_Users ON aspnet_Users.UserId = aspnet_Draws.UserIdWHERE aspnet_Users.UserName = @UserNameGO Also why are you self joining to aspnet_Users? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-29 : 07:52:14
|
See 04/29/2008 : 05:49:18 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonnod123
Starting Member
6 Posts |
Posted - 2008-04-29 : 08:57:08
|
Hi Guys,Got it working! Thanks for all the help. The working SP is:CREATE PROCEDURE [dbo].[aspnet_Draws_CreateDraws]-- Add the parameters for the stored procedure here@NumberOfEntries int, --from form@DrawDate datetime, --date value from form@UserName nvarchar(256) --name of user from formASINSERT INTO aspnet_Draws (UserId,DrawDate,NumberOfEntries)SELECT UserId,@DrawDate,@NumberOfEntriesFROM aspnet_UsersWHERE aspnet_Users.UserName = @UserNameYou were right Visakh, didn't need the inner join at all. Couldn't see the wood for the trees! Thanks again,Jon |
 |
|
|
|