Solved

insert into select from... with modification

Posted on 2004-09-09
5
246 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now