Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL INSERT

Author  Topic 

trogers81
Starting Member

3 Posts

Posted - 2011-04-08 : 10:36:02
Hi All! This is my first thread on the forum.

I am trying to insert three data elements into a table and only want the data to be inserted if a certain user is logged on.

I am using the following script (real names have been ommitted):

INSERT INTO tablename VALUES ('value1', 'value2', 'value3')
SELECT id FROM tablename2
WHERE id = 'somevalue'

Should this give me the result I require?

Thanks for any help in advance.

Trogers

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-08 : 10:51:36
INSERT INTO tablename
SELECT 'value1', 'value2', 'value3'
FROM tablename2
WHERE id = 'somevalue' AND SUSER_SNAME()='myLoginName'

The syntax of that is correct, the version you posted cannot mix the VALUES clause with a SELECT. I'm not sure it meets your needs, other than to limit the INSERT operation to a login named myLoginName.
Go to Top of Page

trogers81
Starting Member

3 Posts

Posted - 2011-04-08 : 11:15:45
Thanks, but what is the SUSER_SNAME() function doing?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-08 : 11:20:02
It returns the login of the current user. You can also use ORIGINAL_LOGIN(). Books Online has more details about both functions.
Go to Top of Page

trogers81
Starting Member

3 Posts

Posted - 2011-04-08 : 11:39:45
Thanks a lot.
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-04-08 : 14:39:17
Better create stored procedure inserting data and give permissions to specific user.

Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page
   

- Advertisement -