Solved

insert into select from... with modification

Posted on 2004-09-09
5
248 Views
Last Modified: 2008-03-06
I want operate a copy data between two table of different DB, were the table are similar bat not identically, and I have to do data validation before insert data.
The query below is my prototype, bat query analyzer return error:The name 'ditta' is not permitted in this context...
My idea is to modify data in select statement as insert table require.

this is the query --------------------------
use dbcamp

INSERT INTO ITAC.dbo.ITAC02_TESTATACOSTI(ITAC02_DITTA_CG18, ITAC02_CODCAMP_ITAC01, ITAC02_CODSTAB_ITMG02, ITAC02_TIPOCF_CG44, ITAC02_CLIFOR_CG44, ITAC02_INDSTATO, ITAC02_PREZZOCAPO, ITAC02_PREZZOKG, ITAC02_FLGPREZZOMOD, ITAC02_FLGCOSTIDET)
VALUES(Q1.ditta,  Q1.CodCampione, Q1.CodStabilimento, Q1.TipoClienteFornitore, Q1.CodiceCliente, Q1.IndStato, 0, 0,0,1)

SELECT
      1 as ditta,
      CodiCamp as CodCampione,
      'ITA' as CodStabilimento,
      
      0 as TipoClienteFornitore,
      
      -- Codice cliente, se null o 0, imposta 11000 cliente generico
      case
      when (CodCliente is null) then
            11000
      when (CodCliente =0) then
            11000
      else
            CodCliente
      end as CodiceCliente,
      3 as IndStato

FROM dbcamp.dbo.ListCamp as Q1
where (CodiCamp  IS not NULL) and cast(CodiCamp as int)<31000
0
Comment
Question by:francecap
  • 2
5 Comments
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12015263
try this:

INSERT INTO ITAC.dbo.ITAC02_TESTATACOSTI(ITAC02_DITTA_CG18, ITAC02_CODCAMP_ITAC01, ITAC02_CODSTAB_ITMG02, ITAC02_TIPOCF_CG44, ITAC02_CLIFOR_CG44, ITAC02_INDSTATO, ITAC02_PREZZOCAPO, ITAC02_PREZZOKG, ITAC02_FLGPREZZOMOD, ITAC02_FLGCOSTIDET)
VALUES(1,  Q1.CodCampione, Q1.CodStabilimento, Q1.TipoClienteFornitore, Q1.CodiceCliente, Q1.IndStato, 0, 0,0,1)

SELECT
     CodiCamp as CodCampione,
     'ITA' as CodStabilimento,
     
     0 as TipoClienteFornitore,
     
     -- Codice cliente, se null o 0, imposta 11000 cliente generico
     case
     when (CodCliente is null) then
          11000
     when (CodCliente =0) then
          11000
     else
          CodCliente
     end as CodiceCliente,
     3 as IndStato

FROM dbcamp.dbo.ListCamp as Q1
where (CodiCamp  IS not NULL) and cast(CodiCamp as int)<31000

0
 
LVL 26

Accepted Solution

by:
Hilaire earned 125 total points
ID: 12015315
You'll have to remove the 'value(...)' and hard-code the 0,0,0,1 in the select statement

INSERT INTO ITAC.dbo.ITAC02_TESTATACOSTI(ITAC02_DITTA_CG18, ITAC02_CODCAMP_ITAC01, ITAC02_CODSTAB_ITMG02, ITAC02_TIPOCF_CG44, ITAC02_CLIFOR_CG44, ITAC02_INDSTATO, ITAC02_PREZZOCAPO, ITAC02_PREZZOKG, ITAC02_FLGPREZZOMOD, ITAC02_FLGCOSTIDET)
SELECT
     1 as ditta,
     CodiCamp as CodCampione,
     'ITA' as CodStabilimento,
     
     0 as TipoClienteFornitore,
     
     -- Codice cliente, se null o 0, imposta 11000 cliente generico
     case
     when (CodCliente is null) then
          11000
     when (CodCliente =0) then
          11000
     else
          CodCliente
     end as CodiceCliente,
     3 as IndStato,
     0, 0, 0, 1
FROM dbcamp.dbo.ListCamp as Q1
where (CodiCamp  IS not NULL) and cast(CodiCamp as int)<31000
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12274686
50/50 split me and Hill, is my suggestion.     We both have submitted valid but slightly different solutions.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 - Merge Replication Issue 1 22
Tsql query 6 22
Sql server get data from a usp to use in a usp 5 16
SQL Function NOT ROUND 9 10
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

777 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