?
Solved

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

Posted on 2014-10-21
5
Medium Priority
?
86 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

800 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