USING CASE STATEMENT IN SQL LOADER CONTROL FILE FIXED WIDTH LENGTH - sql-loader

I have a control file that loads text to oracle but i am trying to populate field C_TIPO_PRATICA with a case statement, please see below control file and the error I receive. what should be the syntax?
ctl file:
LOAD DATA
INFILE *
APPEND
INTO TABLE ANAGRAFICA_INDIRIZZI
(
C_BANCA POSITION (1) CHAR(4),
NDG POSITION (5) CHAR(9),
C_TIPO_PRATICA POSITION (14) CHAR(2) "CASE WHEN C_TIPO_PRATICA IS NULL THEN ' ' ELSE C_TIPO_PRATICA END" ,
N_PRATICA POSITION (16) CHAR(8),
C_TIPO_INDIRIZZO POSITION (24) CHAR(1),
N_GUIDA POSITION (25) CHAR(2),
D_RIFERIMENTO POSITION (27) CHAR(10),
S_TOPONIMO POSITION (37) CHAR(25),
S_INTEST_INDIRIZZO POSITION (62) CHAR(40),
N_CIVICO POSITION (102) CHAR(4),
C_CAP POSITION (106) CHAR(5),
S_DESC_COMUNE POSITION (111) CHAR(40),
C_PROVINCIA POSITION (151) CHAR(2),
C_NAZIONE POSITION (153) CHAR(4),
X_TS_INSERT "SYSDATE"
)
Please help
thanks
I tried to move the clause before the information of position, but it didn't work

Proceed the column name inside the double-quotes with a colon.
"CASE WHEN :C_TIPO_PRATICA IS NULL THEN ' ' ELSE :C_TIPO_PRATICA END"

Related

Error in Copying table in SQL server 2014

I need to remove fields from existing table FullNAICS and copy fields from another table NAICS_NEW. FullNAICS having dependencies and unable to drop the table.I need FullNAICS table name and all fields from NAICS_NEW.
I am trying to copy table but getting the error, Please advise.
Query:
INSERT INTO dbo.FullNAICS
SELECT *
FROM dbo.NAICS_NEW
Error:
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.
I have my table design as follows:
NAICS_NEW Table design:
NAICS_US_CODEvarchar(6)
[INDUSTRY TITLE]varchar(100)
NAICS2d varchar(2)
NAICS3d varchar(3)
NAICS4d varchar(4)
NAICS5d varchar(5)
NAICS6d varchar(6)
[Naics2d.txt]varchar(72)
[Naics3d.txt]varchar(87)
[Naics4d.txt]varchar(95)
[Naics5d.txt]varchar(97)
[Naics6d.txt]varchar(100)
FullNAICS Table design:
NAICSCd6d nvarchar(255) Unchecked
NAICA6dTxt nvarchar(255) Checked
NAICSCd5d nvarchar(255) Checked
NAICS5dTxt nvarchar(255) Checked
NAICSCd4d nvarchar(255) Checked
NAICS4dTxt nvarchar(255) Checked
NAICSCd3d nvarchar(255) Checked
NAICS3dTxt nvarchar(255) Checked
NAICSCd2d nvarchar(255) Checked
NAICS2dTxt nvarchar(255) Checked
[NAICS US CODE] nchar(10) Checked
[INDUSTRY TITLE] nchar(10) Checked
Unchecked
Your tables are using varchar and nvarchar,nchar
if you want to suppress the message use CAST and CONVERT
for all columns
SELECT
CAST(NAICS_US_CODE as nvarchar) ,
CAST([INDUSTRY TITLE] as nvarchar), CAST ....
FROM dbo.NAICS_NEW
CAST and CONVERT (Transact-SQL)
https://msdn.microsoft.com/en-ca/library/ms187928.aspx

Stored procedure in mySQL going wrong

I want to make a stored procedure for my mySQL server.
Here is my procedure
CREATE PROCEDURE ClientInsertProcedure (nomClient CHAR(40), userID INT(4), tel CHAR(10), codePostal CHAR(6), noCivique CHAR(6), r CHAR(30), v CHAR(30), cred DECIMAL(6, 2))
BEGIN
INSERT INTO Entreprise(nom_entreprise, telephone, code_postal, no_civique, rue, ville, credit) VALUES(nomClient, tel, codePostal, noCivique, r, v, cred)
ON DUPLICATE KEY UPDATE telephone = tel, code_postal = codePostal, no_civique = noCivique, rue = r, ville = v, credit = cred
WHERE nom_entreprise = nomClient;
END;
It keep saying that i have an error on last line near END;
I've tried with DELIMITER // and it has change nothing. I also went to mySQL website and i took the syntax there. I also looked Here. Is there any problem with the syntax itself or am i just doing it wrong?
Presumably you want an update on nom_entreprise = nomClient. If so, you need a unique index/constraint on that column:
create unique index idx_ClientInsertProcedure_nomClient
on ClientInsertProcedure(nomClient);
Then, this should work:
CREATE PROCEDURE ClientInsertProcedure (nomClient CHAR(40), userID INT(4), tel CHAR(10), codePostal CHAR(6), noCivique CHAR(6), r CHAR(30), v CHAR(30), cred DECIMAL(6, 2))
BEGIN
INSERT INTO Entreprise(nom_entreprise, telephone, code_postal, no_civique, rue, ville, credit)
VALUES(nomClient, tel, codePostal, noCivique, r, v, cred)
ON DUPLICATE KEY UPDATE telephone = tel, code_postal = codePostal, no_civique = noCivique, rue = r, ville = v, credit = cred;
END;
As a general rule of advice, you should name parameters to stored procedures and functions with something like v_ so you can readily distinguish them from columns in tables.
you can try move away code inside, only
CREATE PROCEDURE ClientInsertProcedure (nomClient CHAR(40), userID INT(4), tel CHAR(10), codePostal CHAR(6), noCivique CHAR(6), r CHAR(30), v CHAR(30), cred
DECIMAL(6, 2))
BEGIN
END;
if no error, that mean your insert with problem
Here is how i got the code working :
I used DELIMITER
As pointed out by Gordon Linoff I wasn't needing a WHERE statement as the item was automatically selected
If so, you need a unique index/constraint on that column
The spacing after DELIMITER is VERY important
DELIMITER //
CREATE PROCEDURE sp_ClientInsert (v_nomClient CHAR(40), v_userID INT(4), v_tel CHAR(10), v_codePostal CHAR(6), v_noCivique CHAR(6), v_rue CHAR(30), v_ville CHAR(30), v_cred DECIMAL(6, 2))
BEGIN
INSERT INTO Entreprise(nom_entreprise, telephone, code_postal, no_civique, rue, ville, credit) VALUES(v_nomClient, v_tel, v_codePostal, v_noCivique, v_rue, v_ville, v_cred)
ON DUPLICATE KEY UPDATE telephone = v_tel, code_postal = v_codePostal, no_civique = v_noCivique, rue = v_rue, ville = v_ville, credit = v_cred;
END //
DELIMETER ;
After some research the DELIMITER keyword make the code act as a single block. On the mySQL website, it specifies that the string "//" (without quotes) after DELIMITER is replace by ";" (without quotes)
The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
This enable a block of code to be execute as a whole.
Thanks for the help.
EDIT
Here is a link that explain INSERT INTO... ON DUPLICATE KEY UPDATE provided by Yu Yenken

Function SQL Server 2008

I created the following function yesterday, and now I am facing an error:
Invalid length parameter passed to the left or substring function
Could you guys have a look at my function?I really appreciate it.
Create function nowFunctionNewadd
(#fladd varchar(255))
returns #tbl table(addr varchar(100), city varchar(100),
state varchar(100), Zip varchar(5))
as
begin
declare #str varchar(100)
,#i int
,#j int
,#str2 varchar(100)
,#address varchar(100)
,#city varchar(100)
,#lastcomma int
,#lastPart varchar(100)
,#zipstart int
,#zip varchar(5) = ''
select #str=rtrim(ltrim(#fladd))
set #i = charindex(',', #str)
set #str2=rtrim(ltrim(substring(#str, #i+1, 999)))
set #j=CHARINDEX(',',#str2)
set #lastcomma = len(#str) - charindex(',', reverse(#str)+',')
set #lastPart = substring(#str, #lastcomma+2, 100)
set #address = REPLACE(rtrim(ltrim(substring(#str,1,#i-1))),',','')
set #zipstart = patindex('%[0-9]%', #lastpart)
set #city=LTRIM(RTRIM(substring(#str, #i+1, #j-1)))
If #zipstart > 0
select #zip = substring(#lastpart, #zipstart, 5),
#lastPart = rtrim(substring(#lastpart, 1, #zipstart-1))
insert into #tbl(addr, city, state, Zip)
values(#address, #city, #lastpart, #zip)
return
end
The problem that I can see with your function starts with this line:
set #j=CHARINDEX(',',#str2)
And then I am guessing that the error is being thrown by this line:
set #city=LTRIM(RTRIM(substring(#str, #i+1, #j-1)))
Your function is working under the assumption that you will have more than one comma present in the string value that you are passing. But if you don't have more than one comma the value for #j will be zero and then you are trying to use a -1 as the length of the city and this will fail throwing the error you are getting.
I created a SQL Fiddle with a demo to work with. Using the address '1234 S.Alameda way,LA,CA12345' your function will work.
But if you change the value to '1234 S.Alameda way,LACA12345' it will fail
See SQL Fiddle Demo
Do you know what the format is going to be for all of the values that you need to pass into the function? If this format is going to change from 1 to 2 or even 3 commas I think you need to rethink how this function is written because it will not work as expected.

error in my first stored procedure--help!

this is my first stored procedure . i find it very difficult to debug it . help me by spending a little time on this
create procedure myworld.perform_target_proc(
IN inp_usr_id integer,
IN inp_tgt_src_id integer,
IN inp_tgt_src_type varchar(30),
IN inp_tgt_usr_id integer,
IN tgt_usr_msg text,
out tgt_res varchar(30)
)
BEGIN
declare target_count integer
select count(target_id) from target where usr_id=inp_usr_id and tgt_src_id=inp_tgt_src_id and tgt_src_type=inp_tgt_src_type
and tgt_usr_id=inp_tgt_usr_id into target_count
if target_count=0 then
begin
insert into target(usr_id, tgt_src_id, tgt_src_type, tgt_usr_id, tgt_usr_msg) values
(inp_usr_id, inp_tgt_src_id, inp_tgt_src_type, inp_tgt_usr_id, inp_tgt_usr_msg)
set tgt_res = 'new target created'
end
else
set tgt_res = 'target already exist'
end if
END |
Looks like you are missing some semicolons.
create procedure myworld.perform_target_proc(
IN inp_usr_id integer,
IN inp_tgt_src_id integer,
IN inp_tgt_src_type varchar(30),
IN inp_tgt_usr_id integer,
IN tgt_usr_msg text,
out tgt_res varchar(30)
)
BEGIN
declare target_count integer;
select count(target_id) from target where usr_id=inp_usr_id and tgt_src_id=inp_tgt_src_id and tgt_src_type=inp_tgt_src_type
and tgt_usr_id=inp_tgt_usr_id into target_count;
if target_count=0 then
insert into target(usr_id, tgt_src_id, tgt_src_type, tgt_usr_id, tgt_usr_msg) values
(inp_usr_id, inp_tgt_src_id, inp_tgt_src_type, inp_tgt_usr_id, inp_tgt_usr_msg)
set tgt_res = 'new target created';
else
set tgt_res = 'target already exist';
end if;
END |
I'm a SQL Server guy, not a MySQL guy, but in SQL Server this wouldn't even compile: variables need # prefix for one thing. My guess is you want
set #target_count = SELECT count(target_id)....
or better yet avoid the local variable altogether.

Why SQL2008 debugger would NOT step into a certain child stored procedure

I'm encountering differences in T-SQL with SQL2008 (vs. SQL2000) that are leading me to dead-ends. I've verified that the technique of sharing #TEMP tables between a caller which CREATES the #TEMP and the child sProc which references it remain valid in SQL2008 See recent SO question.
My core problem remains a critical "child" stored procedure that works fine in SQL2000 but fails in SQL2008 (i.e. a FROM clause in the child sProc is coded as: SELECT * FROM #AREAS A) despite #AREAS being created by the calling parent. Rather than post snippets of the code now, here is another symptom that may help you suggest something.
I fired up the new debugger in SQL Mgmt Studio:
EXEC dbo.AMS1 #S1='06',#C1='037',#StartDate='01/01/2008',#EndDate='07/31/2008',#Type=1,#ACReq = 1,#Output = 0,#NumofLines = 30,#SourceTable = 'P',#LoanPurposeCatg='P'
This is a very large sProc and the key snippet that is weird is the following:
create table #Areas
(
State char(2)
, County char(3)
, ZipCode char(5) NULL
, CityName varchar(28) NULL
, PData varchar(3) NULL
, RData varchar(3) NULL
, SMSA_CD varchar(10) NULL
, TypeCounty varchar(50)
, StateAbbr char(2)
)
EXECUTE dbo.AMS_I_GetAreasV5 -- this child populates #Areas
#SMSA = #SMSA
, #S1 = #S1
, #C1 = #C1
, #Z1 = #Z1
, #SourceTable = #SourceTable
, #CustomID = #CustomID
, #UserName = #UserName
, #CityName = #CityName
, #Debug=0
EXECUTE dbo.AMS_I_GetAreas_FixAC -- this child cannot reference #Areas (in 2008 only!)
#StartDate = #StartDate -- secondarily, I cannot STEP INTO this sProc
, #EndDate = #EndDate
, #SMSA_CD = #SMSA_CD
, #S1 = #S1
, #C1 = #C1
, #Z1 = #Z1
, #CityName = #CityName
, #CustomID = #CustomID
, #Debug=0
-- continuation of the parent sProc**
I can step through the execution of the parent stored procedure. When I get to the first child sproc above, I can either STEP INTO dbo.AMS_I_GetAreasV5 or STEP OVER its execution. When I arrive at the invocation of the 2nd child sProc - dbo.AMS_I_GetAreas_FixAC - I try to STEP INTO it (because that is where the problem statement is) and STEP INTO is ignored (i.e. treated like STEP OVER instead; yet I KNOW I pressed F11 not F10). It WAS executed however, because when control is returned to the statement after the EXECUTE, I click Continue to finish execution and the results windows shows the errors in the dbo.AMS_I_GetAreas_FixAC (i.e. the 2nd child) stored procedure.
Is there a way to "pre-load" an sProc with the goal of setting a breakpoint on its entry so that I can pursue execution inside it?
In summary, I wonder if the inability to step into a given child sproc might be related to the same inability of this particular child to reference a #temp created by its parent (caller).
I found that one of the called stored procedures (i.e. the children) apparently had
SET NOCOUNT OFF
for some long-forgotten reason.
Apparently, the behavior of things in SQL 2008 is quite different when this is in effect.
I ended up identifying all lines like the above in all sProcs, changing them to
SET NOCOUNT ON
..and also, I checked NOCOUNT at the instance level. This fixed these very strange problems.