Solved

Trying to set a variable in a SQL Statement from with the SELECT?

Posted on 2014-10-21
5
80 Views
Last Modified: 2014-10-21
What I need to do is assign the value from "createdBy_ID" to a variable @user_ID but not sure how to do it. Where can I find some good tutorials on PL-SQL? Also, I'm using SQL Server 2012 instead of 2008.

Thanks!

        DECLARE @user_ID               INT=NULL

	IF EXISTS(SELECT userAccount_ID, createdBy_ID FROM USER_Permissions WHERE userAccount_ID = @userAccount_ID AND menuItem_ID = @menuItem_ID)
		BEGIN
			UPDATE	USER_Permissions
			   SET	allowUpdate = @iUpdate
					,allowView = @iView
                                        ,createdBy_ID = @user_ID
					,updatedBy_ID = @updatedBy_ID
					,dateUpdated = GETDATE()
			 WHERE	userAccount_ID = @userAccount_ID AND menuItem_ID = @menuItem_ID
		END
	ELSE

Open in new window

0
Comment
Question by:BlakeMcKenna
5 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40395435
here is the requested code

        DECLARE @user_ID               INT=NULL

	IF EXISTS(SELECT userAccount_ID, createdBy_ID FROM USER_Permissions WHERE userAccount_ID = @userAccount_ID AND menuItem_ID = @menuItem_ID)
		BEGIN
		
		set @user_ID =(SELECT  createdBy_ID FROM USER_Permissions WHERE userAccount_ID = @userAccount_ID AND menuItem_ID = @menuItem_ID)
			UPDATE	USER_Permissions
			   SET	allowUpdate = @iUpdate
					,allowView = @iView
                    ,createdBy_ID = @user_ID
					,updatedBy_ID = @updatedBy_ID
					,dateUpdated = GETDATE()
			 WHERE	userAccount_ID = @userAccount_ID AND menuItem_ID = @menuItem_ID
		END
	ELSE

Open in new window

0
 
LVL 7

Expert Comment

by:slubek
ID: 40395447
I'm not sure what are you trying to achieve, but what about

...
,createdBy_id = SYSTEM_USER
...

Open in new window

?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40395548
If all you are doing is setting the CreatedBy field to the existing value, you can just exclude that field from your update statement.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40395569
>> Where can I find some good tutorials on PL-SQL? <<

Hmm, since PL/SQL is Oracle's language, presumably an Oracle site?

SQL's code, as you've written, is T-SQL (Transact SQL).


@user_ID, and the other variables, needs a value assigned or you will just INSERT NULL values.  You need to either pass those variables as parameters to the proc or assign them values in the proc.

Then, you can just issue the UPDATE without checking first: if there's no matching row, none will be UPDATEd.


UPDATE      USER_Permissions
   SET      allowUpdate = @iUpdate
            ,allowView = @iView
            ,createdBy_ID = @user_ID
            ,updatedBy_ID = @updatedBy_ID
            ,dateUpdated = GETDATE()
  WHERE      userAccount_ID = @userAccount_ID AND menuItem_ID = @menuItem_ID
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40395908
This is what I was looking for. The other answers made sense but the logic of the program is such that I need to handle it in the SP.

Thanks samo!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Managing Columnstore Indexes 2 30
Linked Server Issue with SQL2012 3 26
Help Required 3 97
Why i am getting a star, SSMS does not show me any error. Division Error 5 23
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question