Solved

select values into an "insert"

Posted on 2013-01-23
5
467 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:knowlton
5 Comments
 
LVL 6

Accepted Solution

by:
deiaccord earned 167 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 167 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 166 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:knowlton
ID: 38810966
Thanks!<br /><br />It's nice to have several options!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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