create petl columns if not exists - sql-server

I have a petl table obtained from a database in which I transform and join with data from other databases. The thing is that I need to insert or update data in a new table depending of the fields but the petl table columns doesn't always match with table columns.
Is any method that can create these columns if there are not already in the table? I tried this but returns a FieldSelectionError
table = etl.cut(table, 'Column1', 'Column2', 'Column3', 'Column4')
FieldSelectionError: selection is not a field or valid field index: 'Column4'

Related

SQL Insert does column order matter

I have two tables with the same field names and a stored procedure that updates table B with Table A's data by doing a delete from current table and insert into current table from another table that has update values in it:
delete from ac.Table1
insert into ac.Table1
select *
from dbo.OriginalTable
where dtcreate <getdate()-1
I had to recreate Table1 through GIS software which adds GlobalIDs and an Object ID field. The original order had Object ID at the end and the new table has it at the front. Will this impact executing the SQL statement above?
Yes it will. The order of the columns should match for each value to go in desired column
You can try
Insert into ac.Table1 (column1....columnN)

SSIS Lookup Suggestion

I have a task to Generate a Derived Column (RestrictionID) from a table and add it to a source table if 6 columns on both tables match. The 6 columns include (Country, Department, Account .etc) I decided to go with the SSIS Lookup and generate the Derived column when there's a match. This ID also has a time and amount Limit. Once all the records have ID's, I'm supposed to calculate a running total based on the ID to enforce the limits which is the easy part.
The only problem is this Lookup table changes almost daily and any or all of the 6 columns can have NULLS. Even the completely null rows have an Id. Nulls mean the Restriction is open. eg. If the Country column on one record on the lookup table is null, then the ID of that record can be assigned to records with any country on the source. If one row on the lookup has all null columns, then this is completely open and all records on the source qualify for that ID. The Source table doesn't have NULLS.
Please assist if possible
Thanks
If NULL means any and ignore column in lookup then add this to your where:
use a stored proc and pass your values in and return:
select lookup.ID
from lookup
where #Country = isnull(lookup.Country,#Country) //If lookup inull then it refers to itself creating a 1=1 scenario
and #Department = isnull(lookup.Department,#Department)
and ...

Sql query - How to insert values into multiple rows that have same FK

Got two tables here. Order might contains multiple order_lines.
What I'm trying to do is to create rows in order_line table by using the trigger I created in order table whenever I insert a new row in order table. The number of rows in order_line is same with item_quantity field in order table.
The problem is the trigger can only insert order_line_id and order_id columns without order_item_type and order_item_quantity because these two value is entered from GUI and the trigger can't insert them at the same time it creates new row.
So I need a sql query that can let me insert multiple values into multiple rows that have same order_id one by one.

Insert a FIELD TO A existing table with a condition

I am new to SQL, I am trying to add a column to my table from another table with certain condition
I have Employees2013 table that has StaffNumber column, and I have an Employees table that has StaffNumber and Title columns.
What I am trying to is create a new column called Title in Employees2013 and select title from Employees where Employees2013.StaffNumber = Employees.StaffNumber.
I tried this but it didn't work:
insert into Employees2013(Title)
select e.Title
from LandornetSQL.dbo.Employees e, Employees2013 f
where e.StaffNumber = f.StaffNumber
I get this error:
Cannot insert the value NULL into column 'StaffNumber', table 'xDevProjects.NA\OnderO.Employees2013'; column does not allow nulls. INSERT fails.
Anyone has any idea?
Insert only inserts records, if you want a new column, you must add the column to the table.
ALTER TABLE Employees2013 ADD Title VARCHAR(100)
Then you can update the table, setting the Title column
UPDATE Employees2013 SET Title = Employees.Title
FROM Employees
WHERE Employees.StaffNumber = Employees2013.StaffNumber;

SQL Server get delta items

I have a many-million-records table having an integer primary key defined.
I also have a not-so-many-million IDs that are in the "black list". They are stored in memory (read from a file on disk).
I have to select the records that are NOT in the black list, that is, all the records whose ID is not in my black list.
I solved this using a temp table (single column: ID) to insert the unwanted IDs then select all records whose IDs are not in this table.
My main concern is performance:
Inserting so many record in temp table.
Selecting the item not being in temp table.
EDIT
At the moment I use a temp table like this:
create the temp table with a single column (ID)
fill the temp table with IDs
create a nonclustered index on column
get the delta items with a query similar to this:
select m.id from mytable m where m.id not in (seelct id from #tempTable)
The best option you have here is to add a column to flag each row whether it is blacklisted or not ( ex: call it isBlockListed), and keep this column up-to-date.
You can also add an unclustered index to this flag so you can quickly select your data where isBlockListed = ture / false.

Resources