?
Solved

insert into select from... with modification

Posted on 2004-09-09
5
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

800 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