Solved

insert into select from... with modification

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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