Solved

Insert with Select and Subquery

Posted on 2006-10-18
9
359 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

840 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