Solved

select values into an "insert"

Posted on 2013-01-23
5
484 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 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:Tom Knowlton
ID: 38810966
Thanks!<br /><br />It's nice to have several options!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Sum Over Multiple Tables 20 35
T-SQL: Stored Procedure Syntax 3 34
SQL Server Express or Standard? 5 35
Enabling flash installation using GPO 2 21
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

696 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