Insert with Select and Subquery

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'
)
RVattakunntelAsked:
Who is Participating?
 
Aneesh RetnakaranDatabase AdministratorCommented:
i think the problem is with the statement below this statement , you can check it out
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
Insert into myTable (myId, description, column1, column2, column3)
SELECT 101, DESCRIPTION, 'Column1', Column2, 'Column3
FROM Inserted

0
 
RVattakunntelAuthor 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...
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
RVattakunntelAuthor 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
                  
0
 
RVattakunntelAuthor Commented:
It was complaining in this area...
   
               CURBGUTTERREQD,  --CURBGUTTER REQUIRED?
               null  --STREET CLOSE CREW
               FROM INSERTED
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
RVattakunntelAuthor Commented:
No, there are other update and insert statements as well.
0
 
RVattakunntelAuthor 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...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.