Solved

select values into an "insert"

Posted on 2013-01-23
5
476 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL trigger 5 21
RESTORE A BACKUP IN SQL 2012 from SQL 2008 9 64
SQL Log size 3 17
MS SQL + group by time 4 14
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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 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