Solved

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

Posted on 2014-10-21
5
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach 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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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