?
Solved

Insert with Select and Subquery

Posted on 2006-10-18
9
Medium Priority
?
376 Views
Last Modified: 2012-06-22
Can I write a select statement within an insert statement like this?

Insert into myTable (myId, description, column1, column2, column3)
values
(
 '101',
 select description from inserted,
 'column1',
 select column2 from inserted,
 'column3'
)
0
Comment
Question by:RVattakunntel
[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
  • 4
9 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17763479
Insert into myTable (myId, description, column1, column2, column3)
SELECT 101, DESCRIPTION, 'Column1', Column2, 'Column3
FROM Inserted

0
 

Author Comment

by:RVattakunntel
ID: 17763497
Server: Msg 1046, Level 15, State 1, Procedure fw_trg_insrt_workorder, Line 271
Subqueries are not allowed in this context. Only scalar expressions are allowed.

Received this message...
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17763502
Did you try in the same way i have posted ?(Remember , there is no 'VALUES'  instead i used 'Select' ) can you paste ur Query here
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:RVattakunntel
ID: 17763514
Yes, i think i did it how you suggested...

                  INSERT INTO WORKORDER ( deadend, taskid, internotifthet, stclosename, utilcutsteel, fincntrlid, schedfinish, reportedby, utilcutstreettype, jobcomplete,
                  estatapprmatcost, interruptable, changechildstatus, utilcuttrenbx, contactlastname, utilcutsign, chargestore, sawcutcomp, description, crewfrevl,
                  swrovrground, haschildren, estatapprlabcost, washrockcomp, wo7, cleanupcomp, wo4, wo3, sawcutreqd, asphalt1comp, notifications, washrockreqd, grasscomp,
                  swrovrdisrate, digoutcomp, utilcutdepth, cleanupreqd, asphalt1reqd, utilitycut, streetclosure, contactphonehome, glaccount, utilcutshld, stclosedteclos,
                  actmatcost, orgid, grassreqd, seweroverflow, interconfirmnum, digoutreqd, interst1from, calcpriority, eqnum, ldkey, contactphonecell, actlabcost, phone,
                  pmnum, interdisinfectant, wol4, internotifthetcon, wol3, coldrollcomp, wol2, rampcomp, wol1, interst1to, refhyddeadend, wfactive, wolo3, wolo2, wolo1,
                  parent, historyflag, changedate, coldrollreqd, rampreqd, holefill2comp, statusdate, location, landscapecomp, wo20, holefill2reqd, stclosefrom, crmcase,
                  outmatcost, paversbrickcomp, actlabhrs, landscapereqd, matpupcomp, paversbrickreq, utilcutbarstat, wo12, wo11, interbarricades, asphalttieincomp, actstart,
                  matpupreqd, capcomp, intershtoffcrw, estatapprlabhrs, doeproject, hasfollowupwork, istask, estlabhrs, concrete2comp, internotifynepd, hydrantid, schedstart,
                  asphalttieinreqd, worktype, capreqd, wateroff, remdur, outlabcost, concrete2reqd, estatapprservcost, estservcost, respondby, assetinfo, contactfirstname,
                  interwateroffact, jpnum, outtoolcost, downtime, contactphonebus, holefill1comp, wopriority, targstartdate, actfinish, disabled, internotiffd, actservcost,
                  holefill1reqd, utilcutartery, followupfromwonum, estatapprtoolcost, esttoolcost, internotifynepdcon, crewid, siteid, woeq2, wonum, sidewalkcomp, contract,
                  topsoilcomp, utilcutlength, interrupt, incidentaddress, utilcutwidth, interst1name, sidewalkreqd, acttoolcost, internotiffdcon, topsoilreqd, interdigtess,
                  concrete1comp, interpipeoff, estmatcost, utilcutrnttot, drivewaycomp, asphalt2comp, othercomp, status, swrovrpntrep, interwaterloss, concrete1reqd, damage,
                  estdur, failurecode, stcloseto, curbguttercomp, problemcode, drivewayreqd, estlabcost, changeby, asphalt2reqd, otherreqd, targcompdate, emailoncharge,
                  eqlocpriority, reportdate, curbgutterreqd, stclosecrw )
                  SELECT 'N',
                  null,
                  null,
                  null,
                  'N',
                  null,
                  null,
                  'SYSADM',
                  null,
                  'N',  -- JOBCOMPLETE
                  '0.0',
                  'N',
                  'N',
                  'N',
                  null,
                  'N',
                  'N',
                  'N',
                  DESCRIPTION,  -- DESCRIPTION
                  null,
                  null,
                  'N', -- HAS CHILDREN?
                  '0.0',
                  'N', -- WASHROCK COMPLETE?
                  null,
                  'N', -- CLEANUP COMPLETE?
                  null,
                  null,
                  SAWCUTREQD,  -- SAWCUT REQUIRED?
                  'N',  --ASPHALT1COMP?
                  'N', -- NOTIFICATIONS
                  WASHROCKREQD, -- WASHROCK REQUIRED?
                  'N', --GRASS COMPLETE?
                  null,
                  'N',  --DIGOUT COMPLETE?
                  null,
                  CLEANUPREQD, --CLEANUP REQUIRED?
                  ASPHALT1REQD, --ASPHALT1REQD
                  'N', --UTILITYCUT
                  'N', -- STREETCLOSURE
                  null,
                  null,
                  'N',  --UTILCUTSHLD
                  null,
                  '0.0',
                  'FTWORG', --ORGID
                  GRASSREQD,  --GRASS REQUIRED?
                  'N',  --SEWEROVERFLOW?
                  null,
                  DIGOUTREQD,  --DIGOUT REQUIRED?
                  null,
                  '3', --CALCPRIORITY
                  null,
                  null, --LDKEY
                  null,  --CONTACT CELL PHONE
                  '0.0',
                  null,
                  null,
                  'N',
                  null, --WOL4
                  null,
                  null, --WOL3
                  'N',  --COLDROLLCOMP
                  null, --WOL2
                  'N',  -- RAMPCOMP?
                  null,  -- WOL1
                  null,
                  'N',
                  'N',
                  null,  --WOLO3
                  'UTILCUT',  --WOLO2 SEC
                  'FOSUPP', --WOLO1 DEPT
                  WONUM,  --PARENT WORKORDER
                  'N',  --HISTORY FLAG
                  GETDATE(), --CHANGEDATE
                  COLDROLLREQD, --COLDROLL REQURIED?
                  RAMPREQD, -- RAMP REQUIRED?
                  'N',  -- HOLE FILL2 COMPLETE?
                  GETDATE(),  --STATUSDATE
                  LOCATION,  --LOCATION
                  'N',  --LANDSCAPE COMPLETE?
                  'N', --WO20
                  HOLEFILL2REQD,  --HOLEFILL2REQUIRED?
                  null,
                  null,  --CRMCASE
                  '0.0',
                  'N',  --PAVERSBRICKCOMP
                  '0.0',
                  LANDSCAPEREQD,  --LANDSCAPEREQUIRED
                  'N',  --MATPICKUP COMP
                  PAVERSBRICKREQD,  --PAVERSBRICK REQUIRED
                  null,
                  null,  --WO12
                  null,  --WO11
                  'N',
                  'N',  --ASPHALTTIEIN COMPLETE?
                  null,  --ACTUAL START
                  MATPUPREQD,  --MATERIAL PICKUP REQUIRED??
                  'N',  --CAP COMPLETE?
                  null,
                  '0.0',
                  null,  --DOE PROJECT
                  'N',  --HAS FOLLOW UP WORK
                  'N',  -- HAS TASK
                  '0.0',
                  'N',  --CONCRETE 2 COMPLETE?
                  null,
                  null,
                  null,
                  ASPHALTTIEINREQD,  --ASPHALTTIEINREQUIRED
                  'RW', --WORKTYPE
                  CAPREQD,  --CAPREQUIRED
                  'N',  --WATEROFF
                  null,
                  '0.0',
                  CONCRETE2REQD,  --CONCRETE 2 REQUIRED
                  '0.0',
                  '0.0',
                  GETDATE(), --RESPONDDATE
                  'N', --ASSETINFO
                  null,
                  null,
                  null,
                  '0.0',
                  'N',
                  null,
                  'N',  --HOLEFILL1 COMPLETE?
                  '3',  --WORKORDER PRIORITY
                  null,
                  null,
                  'N',
                  null,
                  '0.0',
                  HOLEFILL1REQD,  --HOLEFILL1 REQUIRED
                  'N',
                  null,
                  '0.0',
                  '0.0',
                  null,
                  null,  --CREWID
                  'FTW', --SITEID
                  null,  --WOEQ2
                  @WONUM,  --WONUM
                  'N',  --SIDEWALKCOMPLETE?
                  null,
                  'N', --TOPSOIL COMPLETE?
                  null,
                  'N',
                  NULL, --INCIDENT ADDRESS
                  null,
                  null,
                  SIDEWALKREQD,  --SIDEWALK REQUIRED
                  '0.0',
                  null,
                  TOPSOILREQD,  --TOPSOIL REQUIRED?
                  null,
                  'N',  --CONCRETE 1 COMPLETE
                  null,
                  '0.0',
                  null,
                  'N',  --DRIVEWAY COMPLETE?
                  'N', --ASPHALT2 COMPLETE?
                  'N',  -- OTHER COMPLETE?
                  '1A-NEW', --STATUS
                  'N',
                  null,
                  CONCRETE1REQD,  --CONCRETE1 REQUIRED?
                  'N',  --DAMAGE
                  '0.0',
                  null,  --FAILURECODE
                  null,
                  'N',  --CURBGUTTER COMPLETE?
                  null,  --PROBLEM CODE
                  DRIVEWAYREQD,  --DRIVEWAY REQUIRED?
                  '0.0',
                  'MAXIMO', --CHANGEBY
                  ASPHALT2REQD,  --ASPHALT2 REQUIRED?
                  OTHERREQD, --OTHER REQUIRED
                  null,
                  'N',
                  null,
                  GETDATE(), --REPORTDATE
                  CURBGUTTERREQD,  --CURBGUTTER REQUIRED?
                  null  --STREET CLOSE CREW
                  FROM INSERTED
                  
0
 

Author Comment

by:RVattakunntel
ID: 17763520
It was complaining in this area...
   
               CURBGUTTERREQD,  --CURBGUTTER REQUIRED?
               null  --STREET CLOSE CREW
               FROM INSERTED
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17763539
Is this the only query in that trigger ...

Also if the columns allows null, then you dont have to explicitly insert nulls to those columns, You just have to mention the 'not null' columns in the insert list and put values for them in the 'SELECT' list
0
 

Author Comment

by:RVattakunntel
ID: 17763552
No, there are other update and insert statements as well.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 17763557
i think the problem is with the statement below this statement , you can check it out
0
 

Author Comment

by:RVattakunntel
ID: 17763572
Yes, there was an INSERT statement two steps down and it was the one causing the problem.  Thank you very much for your help...
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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
Suggested Courses

752 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