[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

insert into select from... with modification

Posted on 2004-09-09
5
Medium Priority
?
255 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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 month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

649 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