Insert empty string to Oracle database from SQL Server result strange characters - sql-server

I have two databases Oracle (10.2.0.4) and SQL Server (2008 R2).
When I insert data through linked server:
EXECUTE( 'begin INSERT INTO TEST_TABLE(ID,data1,NIP) VALUES(?,sysdate,?); end;',
'',
''
) AT LS_ORACLE;
result in TEST_TABLE as:
id | data1 | NIP
-----------------
ǧ |16/07/21|
Empty string is convert to "ǧ".
How to eliminate this strange behavior?
For information:
CREATE TABLE "TEST_TABLE"
( "ID" VARCHAR2(200 BYTE),
"DATA1" DATE,
"NIP" VARCHAR2(200 BYTE)
)

you can use my workaround.
When you'll use variables this problem is solved
declare #id nvarchar(50) = ''
,#nip nvarchar(50) = ''
EXECUTE( 'begin INSERT INTO TEST_TABLE(ID,data1,NIP) VALUES(?,sysdate,?); end;',
#id,
#nip
) AT LS_ORACLE

Related

Perl, SQL Server use of ##IDENTITY

I have a SQL Server table with an identity column, set to autoincrement.
Coded in Perl, the insert in the code below works fine, in the while loop the fetchrow_array() call returns no data in the #row array.
How do I best retrieve the identity value for use in subsequent SQL statements?
my $term_sql = "INSERT INTO reminder_term(site, name, description, localization) OUTPUT \#\#IDENTITY VALUES(?,?,?,?)";
my $t_stmt = $dbh->prepare($term_sql);
...
$t_stmt->execute($site, $name, $description, $localizer);
while (#row = $t_stmt->fetchrow_array()) {
$referential_key = $row[0];
}
Avoid using the ##IDENTITY value since it's unreliable in the presence of triggers.
Given the example table schema...
create table [dbo].[reminder_term] (
[id] int not null identity(1,1),
[site] nvarchar(10),
[name] nvarchar(10),
[description] nvarchar(10),
[localization] nvarchar(10)
);
You can rework your OUTPUT clause slightly you can capture the new id value by way of the special inserted row source...
INSERT INTO reminder_term(site, name, description, localization)
OUTPUT inserted.id
VALUES(?,?,?,?)

Querying into a memory table

SQL Server lets you create in-memory tables. But how do you do insert into operation on that?
So for example, I used this code to create my type:
CREATE TYPE dbo.typeTableDelimetedFileSpec
AS TABLE
(
TemplateId INT NOT NULL,
FieldName VARCHAR(50) NOT NUL,
FieldPosition SMALLINT NOT NULL INDEX fpos
)
WITH (MEMORY_OPTIMIZED = ON);
Then, I tried to do this:
DECLARE #T [dbo].[typeTableDelimetedFileSpec]
SELECT *
INTO #T
FROM [dbo].[_DelimetedFileSpec]
WHERE TemplateId = 1
I know the structures match (_DelimetedFileSpec does not have index fpos, but other than that there are no differences).
I get:
Incorrect syntax near '#T'.
Also, just to check out that there are no other errors, I confirmed that the following works fine:
SELECT *
INTO #x
FROM [dbo].[_DelimetedFileSpec]
WHERE TemplateId = 1
Is it possible to somehow insert directly into the memory-table, like this?
I found a way to do it efficiently!
Declare #DeliSpecs [dbo].[typeTableDelimetedFileSpec]
Insert into #DeliSpecs (TemplateId, FieldName, FieldPosition) Select TemplateId, FieldName, FieldPosition from _DelimetedFileSpec where TemplateId = #Id

I am getting following Error message while creating IBM db2 procedure

What is wrong with the following code?
PROCEDURE DATETYPE DYNAMIC RESULT SETS 1 LANGUAGE SQL
BEGIN
DECLARE #DateType TABLE
( LABEL CHAR(30) ,
Value VARCHAR(1) );
DECLARE C CURSOR WITH RETURN FOR;
INSERT
INTO
#DateType
VALUES ('Paid Dates Only',
'P') INSERT
INTO
#DateType
VALUES('Incurred Dates with Paid',
'S') SELECT
*
FROM
#DateType;
OPEN C;
END
I am using db2 and gets the following error:
SQL Error [42601]: An unexpected token "( LABEL CHAR(30) , Value VARCHAR(1) )" was found following " ". Expected tokens may include: "E #DateType TABLE ".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.21.29
wichever DB2 it is (db2-400 or db2-luw) DECLARE xx TABLE is not DB2 SQL PL syntax
Btw if I understand it well your code can be replaced with a view
CREATE OR REPLACE VIEW DATETYPE (LABEL, VALUE) as (
VALUES
('Paid Dates Only', 'P'),
('Incurred Dates with Paid', 'S')
)
edit:since it has to be a prodecure, then you can use
CREATE OR REPLACE PROCEDURE DATETYPE ()
RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE DATAS CURSOR WITH RETURN FOR
VALUES
('Paid Dates Only', 'P'),
('Incurred Dates with Paid', 'S');
OPEN DATAS;
END

SQL Server Import Wizard treats NULL as literal string 'NULL'

When I attempt to import a .csv comma-delimited flat file into a Microsoft SQL server 2008R2 64-bit instance, for string columns a NULL in the original data becomes a literal string "NULL" and in a numeric column I receive an import error. Can anyone please help???
KISS
Pre-process it, Replace all "NULL" with "".
ie the .csv file will have
,,
Instead of
NULL,NULL,
Seems to do the job for me.
Put the data into a staging table and then insert to the production table using SQL code.
update table1
set field1 = NULL
where field1 = 'null'
Or if you want to do a lot of fields
update table1
set field1 = case when field1 = 'null' then Null else Field1 End
, field2 = case when field2 = 'null' then Null else Field2 End
, field3 = case when field3 = 'null' then Null else Field3 End
Adding to HLGEM's answer, I do it dynamically, I load into staging table here all column types are VARCHAR and then do:
DECLARE #sql VARCHAR(MAX) = '';
SELECT #sql = CONCAT(#sql, '
UPDATE [staging].[',[TABLE_NAME],']
SET [',[COLUMN_NAME],'] = NULL
WHERE [',[COLUMN_NAME],'] = ''NULL'';
')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE [TABLE_SCHEMA] = 'staging'
AND [TABLE_NAME] IN ('MyTableName');
SELECT #sql;
EXEC(#sql);
Then do:
INSERT INTO [dbo].[MyTableName] ([col1], [col2], [colN])
SELECT [col1], [col2], [colN]
FROM [staging].[MyTableName]
Where table [dbo].[MyTableName] is defined with the desired column types, this also fails and tells you in type conversion errors...

How do you pass collections of data into a T-SQL stored procedure in SQL Server 2003

I have a stored procedure that ideally should be able to accept a list/table of NVARCHARs from the database client. I'm aware of table parameters in SQL Server 2008 but I'm stuck with running SQL Server 2003.
Currently, I'm concatenating the strings with a separator character on the client side, passing the resulting string as a NVARCHAR parameter, and then teasing apart the string on entry to the stored procedure, but this leaves much to be desired.
Have you looked at passing in XML?
So, for XML a little like:
<ArrayOfService xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Service Id="2" Name="AUSTRALIA" Code="AUS" />
<Service Id="10" Name="FAR EAST" Code="FEE" />
</ArrayOfService>
In SQL Server 2005 you could do:
-- Lookup Services
DECLARE #ServiceXml AS XML
CREATE TABLE #Service
(
Id INT,
[Name] VARCHAR( 50 ),
Code VARCHAR( 10 )
)
INSERT INTO #Service
(
Id,
[Name],
Code
)
SELECT
CASE
WHEN LegsTbl.rows.value('#Id', 'nvarchar(255)') = '' THEN NULL
WHEN LegsTbl.rows.value('#Id', 'int') = 0 THEN NULL
ELSE LegsTbl.rows.value('#Id', 'int')
END AS Id,
LegsTbl.rows.value('#Name', 'varchar(50)') AS [Name],
LegsTbl.rows.value('#Code', 'varchar(50)') AS TopazCode
FROM
#ServiceXml.nodes('/ArrayOfService/Service') LegsTbl(rows)
Or SQL Server 2000:
DECLARE #ServiceXml AS NTEXT
DECLARE #iServiceXml AS INT
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument #iServiceXml OUTPUT, #ServiceXml
CREATE TABLE #Service
(
Id INT,
[Name] VARCHAR( 50 ),
Code VARCHAR( 10 )
)
INSERT INTO #Service
(
Id,
[Name],
Code
)
SELECT
Id,
Name,
Code
FROM
OPENXML( #iServiceXml, '/ArrayOfService/Service', 3)
WITH (Link8Id int '#Id',
Name varchar(50) '#Name',
Code varchar(10) '#TopazCode')
IN SQL2005 (which I assume you mean?) I build up an XML string and pass it
CREATE PROCEDURE dbo.ig_SelectRecentConfigurableAppsByMakes
(
#MakesXML XML, -- <makes><value>GMC</value>...</makes>
#TopN INT
)
AS
DECLARE #Makes TABLE (Make NVARCHAR(30))
INSERT INTO #Makes
SELECT ParamValues.make.value('.','NVARCHAR(30)')
FROM #MakesXML.nodes('makes/value') AS ParamValues(make)
;

Resources