Solved

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

Posted on 2014-10-21
5
79 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert SP in a format for debugging 7 30
Join vs where 2 38
Changing multiple SQL login passwords 9 31
Getting same value for every field in SQL 2 27
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

22 Experts available now in Live!

Get 1:1 Help Now