Solved

select values into an "insert"

Posted on 2013-01-23
5
444 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:ScottPletcher
ScottPletcher 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 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