?
Solved

select values into an "insert"

Posted on 2013-01-23
5
Medium Priority
?
500 Views
Last Modified: 2013-01-23
I want to take these values and insert them into another table:

sel into

The menuids are not guaranteed to be 531 and 532, so I need to use the select to find-out what they are by finding the values of other columns on the same row.

But I don't want to just insert the menuids, I want to insert other values as well that do not come from a table but will be hard-coded.

Not sure how to do this.


the resulting action would be essentially doing this:

insert into menuperm (menuid, clientid, clientsid, roleid, setting)
values (<<first menu id from the select>>, 0, 0, 3, 1)

insert into menuperm (menuid, clientid, clientsid, roleid, setting)
values (<<first menu id from the select>>, 0, 0, 4, 1)

insert into menuperm (menuid, clientid, clientsid, roleid, setting)
values (<<second menu id from the select>>, 0, 0, 3, 1)

insert into menuperm (menuid, clientid, clientsid, roleid, setting)
values (<<second menu id from the select>>, 0, 0, 4, 1)


So there will be 2 sets of inserts for each menu id returned from the select I described.
0
Comment
Question by:Tom Knowlton
[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 6

Accepted Solution

by:
deiaccord earned 668 total points
ID: 38810750
You need the insert-select fomat for you query, and 'hard-code' you values is the subselect. The quickest way using 2 queies for each coding

e.g.

insert into menuperm (menuid, clientid, clientsid, roleid, setting)
select menuid, 0, 0, 3, 1
from Menu 
where MenuCode in ('AgentFormAdd','AgentFormEdit')

insert into menuperm (menuid, clientid, clientsid, roleid, setting)
select menuid, 0, 0, 4, 1
from Menu 
where MenuCode in ('AgentFormAdd','AgentFormEdit')

Open in new window

0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 668 total points
ID: 38810770
so you want to insert two rows for each, say 531 will have a 3 and a 4? you can do something like

insert menuperm
select menuid, 0, 0, 3, 1  from Menu
where MenuCode in ('...... complete....
union all
select menuid, 0, 0, 4, 1  from Menu
where MenuCode in ('...... complete....
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 total points
ID: 38810803
--INSERT INTO dbo.menuperm (menuid, clientid, clientsid, roleid, setting) --uncomment after testing
SELECT
    m.menuid, 0 AS clientid, 0 AS clientsid, valuelist.roleid, 1 AS setting
FROM dbo.Menu m
CROSS JOIN (
    SELECT 3 AS roleid UNION ALL
    SELECT 4
) AS valuelist
WHERE
    m.MenuCode IN ('AgentFormAdd', 'AgentFormEdit')
ORDER BY --optional
    m.menuid, valuelist.roleid
0
 
LVL 6

Expert Comment

by:deiaccord
ID: 38810828
Just to add ralmada's version would be more atomic than using 2 seperate inserts so would be a more preferable way of acheiving what you want.

ScottPletchers version should be faster still as only 1 select from the Menu table is specified (though this would only be noticable if the table is very large)
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 38810966
Thanks!<br /><br />It's nice to have several options!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

764 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