we want to rearrange the column order in the insert command in LKM.
want to put the condition for the column list with its datatype equal blob.
insert <%
if ("1".equals(odiRef.getOption ("LOAD_DIRECT"))) {
out.print("/*+ APPEND_VALUES */");
};%> into <%=odiRef.getTable("L", "COLL_NAME", "A")%>
(
<%=odiRef.getColList("", "[CX_COL_NAME]", ",\n\t", "","")%>
)
values
(
<%=odiRef.getColList("", ":[CX_COL_NAME]", ",\n\t", "","")%>
)
we rearrange the columns on both source and target model and the create staging table and insert staging and target table order changed and worked.
Related
I have a large SQL INSERT that I need to do.
The table is called testResult and has an id, name and value column.
The insert looks like this:
INSERT INTO testResult ( name, value )
VALUES
('Helium', '.001'),
('Oxygen', '.19'),
('Palladium', '.054'),
('Carbon', '.21'),
...etc
The id is an identity type column and is auto generated by Sql Server.
Is there a way to return the inserted values along with the auto-generated Id after I do the insert?
Thanks!
Yes, the OUTPUT clause does exactly that.
Example:
INSERT INTO testResult (name, value)
OUTPUT INSERTED.*
VALUES
('Helium', '.001'),
('Oxygen', '.19'),
('Palladium', '.054'),
('Carbon', '.21');
You can specify exact column names, like INSERTED.id.
This is all done within one SQL statement from your end, without the need to write separate SELECT.
Check more in the docs
I'm copying the contents of a table into another identical table. But there are already data in the destination table.
Some data in the destination table has the same code as the source table.
Is it possible to skip the duplicates and not to block the insertion for the rest of the data without it failing?
insert into [DB2].[dbo].[MAN] values([MAN],[DES])
SELECT [MAN]
,[DES]
FROM [DB1].[dbo].[MAN]
You can use NOT EXISTS :
INSERT INTO [DB2].[dbo].[MAN] ([MAN], [DES])
SELECT M.[MAN], M.[DES]
FROM [DB1].[dbo].[MAN] AS M
WHERE NOT EXISTS (SELECT 1 FROM [DB2].[dbo].[MAN] M1 WHERE M1.COL = M.COL);
You need to change the M1.COL = M.COL with your actual column name from which you can identify the duplicate values.
If you have your unique col then you can go like this.
insert into [DB2].[dbo].[MAN] values([MAN],[DES])
SELECT [MAN]
,[DES]
FROM [DB1].[dbo].[MAN] WHERE uniqueCol NOT IN (SELECT uniqueCol FROM [DB2].[dbo].[MAN])
Otherwise append few columns to get unique one and compare like that.
I am using SSIS to move excel data to a temp sql server table and from there to the target table.
So my temp table consists of only varchar columns - my target table expects money values for some columns. In my temp table the original excel columns have a formula but leave an empty cell on some rows which is represented by the temp table with an empty cell as well. But when I cast one of these columns to money these originally blank cells become 0,00 in the target column.
Of course that is not what I want, so how can I get NULL values in there? Keeping in mind that it is possible that a wanted 0,00 shows up in one of these columns.
I guess I would need to edit my temp table to turn the empty cells to NULL. Can I do this from within a SSIS package or is there a setting for the table I could use?
thank you.
For existing data you can write a simple script that updates data to NULL where empty.
UPDATE YourTable SET Column = NULL WHERE Column = ''
For inserts you can use NULLIF function to insert nulls if empty
INSERT INTO YourTable (yourColumn)
SELECT NULLIF(sourceColum, '') FROM SourceTable
Edit: for multiple column updates you need to combine the two solutions and write something like:
UPDATE YourTable SET
Column1 = NULLIF(Column1, '')
, Column2 = NULLIF(Column2, '')
WHERE Column1 = '' OR Column2 = ''
etc
That will update all
I'm working on an sp that inserts data into two tables. The two tables are featured and featured type, both have a pk featuredid that gets auto incremented each time something is added. I have:
insert into featured
(title,text,imageURL, priority )
values
(#title,#text,#imageURL, #priority),
insert into featuredtype
(loginPage, indexPage, mobilePage)
values
(#loginPage, #indexPage, #mobilePage)
However, it appears this is not the correct method for inserting into two tables from one sp.
You need to get rid of the comma after #priority),
You can replace it with a semi-colon ; or nothing at all, so
create proc yourproc
(
-- parameter definitions here
)
as
begin
insert into featured
(title,text,imageURL, priority )
values
(#title,#text,#imageURL, #priority)
insert into featuredtype
(loginPage, indexPage, mobilePage)
values
(#loginPage, #indexPage, #mobilePage)
end
You can even try with
insert into featured (title,text,imageURL, priority )
select #title,#text,#imageURL, #priority
insert into featuredtype (loginPage, indexPage, mobilePage)
select #loginPage, #indexPage, #mobilePage
Here is my "upsert" code:
UPDATE LastTicket SET LastTicketNumber=LastTicketNumber+1
OUTPUT INSERTED.LastTicketNumber WHERE CategoryId='1';
IF ##ROWCOUNT=0 INSERT INTO LastTicket (CategoryId,LastTicketNumber)
OUTPUT INSERTED.LastTicketNumber VALUES ('1','2')
So, when the row exists, it successefully updates, the OUTPUT returns the new, incremented LastTicketNumber.
On the other hand, when the row does not exist, the sql server successefully creates it and populates with the data I am passing to SqlCommand (1,2). So, it creates the row, but returns null. Meaning nothing! Why is that? And why when i replace the "INSERTED.LastTicketNumber" with the "INSERTED.CategoryId" is BEGINS to return not-null, the category id. Why is that? And how to return what I need?
The table has only these two columns and nonclustered primary composite key on both of them.
(MSSQL 2008)
If no row exists in the table, the first time the batch runs it will return two result sets - the first being empty (because there is no row to update) and the second containing the inserted Id.
Perhaps you are seeing the first result set and not the second.
Try the following:
DECLARE #t table (LastTicketNumber int)
UPDATE LastTicket SET LastTicketNumber=LastTicketNumber+1
OUTPUT INSERTED.LastTicketNumber INTO #t (LastTicketNumber) WHERE CategoryId='1';
IF ##ROWCOUNT=0 INSERT INTO LastTicket (CategoryId,LastTicketNumber)
OUTPUT INSERTED.LastTicketNumber INTO #t (LastTicketNumber) VALUES ('1','2')
select LastTicketNumber from #t