Solved

Insert with Select and Subquery

Posted on 2006-10-18
9
353 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 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