Solved

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

Posted on 2014-10-21
5
77 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:ScottPletcher
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now