We help IT Professionals succeed at work.

Insert with Select and Subquery

RVattakunntel
on
Medium Priority
433 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'
)
Comment
Watch Question

AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
Insert into myTable (myId, description, column1, column2, column3)
SELECT 101, DESCRIPTION, 'Column1', Column2, 'Column3
FROM Inserted

Author

Commented:
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...
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
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
                  

Author

Commented:
It was complaining in this area...
   
               CURBGUTTERREQD,  --CURBGUTTER REQUIRED?
               null  --STREET CLOSE CREW
               FROM INSERTED
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
No, there are other update and insert statements as well.
Database Consultant
CERTIFIED EXPERT
Top Expert 2009
Commented:
i think the problem is with the statement below this statement , you can check it out

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Yes, there was an INSERT statement two steps down and it was the one causing the problem.  Thank you very much for your help...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.