can't insert records in new table from existing table in sql server 2005 - sql

I need to insert records into a new table from an existing table. I used the following query to do this:
Insert into Newtable
Select * from Oldtable where date1 = #date
This query works most of the time, but in one scenario I get 10 million records to be inserted for the date1 value. In this case I'm getting the following error message:
Error : The transaction log for database "tempDB" is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases
Should I break the query into parts and insert them sequentially, or is there a way to do this with the current query?

This is, perhaps, a distasteful suggestion. But, you can try exporting the data to a file and then inserting using bulk-insert, with database logging set to SIMPLE or BULK-LOGGED.
More information is at http://msdn.microsoft.com/en-us/library/ms190422.aspx.

Related

Teradata query using a large list of values in WHERE predicate

I'm attempting to query Teradata in SQL Assistant, something along the lines of
select * from
addressTable
where accountNumber in
(
'AC001',
'AC098',
'AC711',
...
)
from a list of over 100,000 accountNumbers sent to me in a file.
What I have tried
StackOverflow past questions - all seem to say that you need to insert into a temp table using a select clause from a table within the same database
Teradata doco on the link below. When I go to create table globdb.gt1, I get error 'CREATE TABLE Failed 3802: Database 'globdb' does not exist.
https://docs.teradata.com/r/Teradata-Database-SQL-Fundamentals/June-2017/Database-Objects/Tables/Global-Temporary-Tables

ERROR HANDLING and Loading into target Table using SQL

I have a scenario Today where I am stucked Up little bit. I have data like this below in my Source Oracle Source Table(Sample Data)
ID,NAME,SALARY,BIRTHDAY
1,ABHIJIT,2000,17/12/1990
2,ROHIT,-2000,13/11/1988
3,MOHIT,500,2075-575-43
Now Salary in the 2nd Row is negetive and BIRTHDAY in the 3rd Row is an Invalid Format(Valid is MM/dd/yyyy). Both 2nd and 3rd Row should go to INVALID_EMPLOYEE and 1st record should go to VALID_EMPLOYEE. In the Source File Date Format is coming as dd/mm/yyyy. Which I have to convert to MM/dd/yyyy format and also have to check whether Date Format incoming in Source File is dd/mm/yyyy or not. Salary should not be less than 0. Source Table All columns are in String and in Target Table ID is Integer,NAME as VARCHAR2(255), SALARY is NUMBER and BIRTHDAY is DATE.
I have handled all this in my Project ETL Tool. So I am trying to push all this in Query to improve performance.Any help is much appreciated
Although the error records should be ideally handled by the process that's doing the insert, in many cases this may not be possible to do.
Instead what you can do now is to set up constraints on the table for the Salary column. The date checking can be implemented by simply having the column of the date datatype. Then create a error logging table to log the errors.
Now we need a table to log your invalid records. For this you have two options.
You can have the INVALID_EMPLOYEES table itself as the error table. But that would mean having additional columns in the table (ORA_ERR_MESG$ and ORA_ERR_TAG$).
If having additional columns is not an option then you can create a dedicated error table and then move the records from this table to your INVALID_EMPLOYEES table. You will have to write some code that runs after your insert code to move the records.
See the DBMS_ERRLOG package to see how to create the error logging tables.
Once you have your error logging tables created you have to instruct your INSERT code to log the errors into your error logging table. This will ensure that all the valid records get inserted in your employees table and the invalid records go to your error table with a column that stores the error that was encountered while inserting that particular record. So your insert code would look something like
INSERT INTO VALID_EMPLOYEES
SELECT ID,
NAME,
SALARY,
BIRTHDAY
FROM my_source_table
LOG ERRORS INTO INVALID_EMPLOYEES ('Oops this guy failed');
In this case 'Oops this guy failed' would go into the ORA_ERR_TAG$ column of the error table. You can use this tag column to identify different insert statements that tried to insert an invalid record.
Hope that helps!

How to trace SQL statements for a specific table?

I'm currently working with DB2 v10.5 and I need to log all SQL Statements that occurr on a specific table (A). For instance, if an INSERT occur on table A, I need to "grab" that SQL Statement and log it into another table (A_LOGGER).
The solution I've reached was to create a TRIGGER (for each CRUD operation) over table A that looks into the table SYSIBMADM.SNAPDYN_SQL and try to save the last executed statement on table A.
Example for the INSERT Statement:
CREATE OR REPLACE TRIGGER OPERATIONS_INSERT_TRIGGER
AFTER INSERT ON REPLDEMO.OPERATIONS
REFERENCING NEW AS OBJ
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
INSERT INTO REPLDEMO.OPERATIONS_LOGGER (LAST_SQL_STATEMENT)
SELECT STMT_TEXT FROM SYSIBMADM.SNAPDYN_SQL
WHERE 1=1
AND STMT_TEXT LIKE 'INSERT INTO REPLDEMO.OPERATIONS (%'
AND STMT_TEXT NOT LIKE '%?%';
END%
But looking at table SYSIBMADM.SNAPDYN_SQL is not the best solution because you cannot guarantee that you'll get the truly last SQL Statement executed on table A. Moreover, if there's a massive number of sql statements executed over table A in a very short period the TRIGGER will replicate many of the statements already saved on A_LOGGER.
So, my question is: Is there an effective and secure way to get the last SQL Statement executed over a table?
Thanks.

SQL help extracting data from one table and based on a backup table, inserting that data

I have the following SQL (SSMS) statement that returns invalid records for endstrands not found in the fiberstrands tabl:
SELECT * FROM FIBERSPLICE fs
WHERE ENDSTRAND NOT IN (SELECT ID FROM FIBERSTRAND ft)
Every record returned here needs to be rectified and placed back into the fiberstrand table based off a backup of the fiberstrand table.
Now, I have a backup table, FiberStrandHAS, that has all of the fiberstrand records that are missing (plus more) as indicated by the above statement. My goal is to insert the specified records from the above statement using the FiberStranHAS backup table into the fiberstrand table. Any ideas on how I could accomplish this task?
Figured out how to get what I needed...
select *
from fiberstrandhas fsh
inner join (SELECT * FROM FIBERSPLICE fs WHERE ENDSTRAND NOT IN (SELECT ID FROM FIBERSTRAND ft)) es
on (fsh.id = es.endstrand)

Update trigger with GROUP BY

I'm using insert-/update triggers to update a second table's column Price.
The insert trigger seems to work perfectly, but when I try to change a single record in SSMS, I get an error:
The row value(s) updated or deleted either do not make the row
unique or they alter multiple rows(2 rows).
This is my update-trigger:
CREATE TRIGGER [dbo].[trgUpdateMasterData] ON [dbo].[tabSparePartMasterData_Temp]
AFTER UPDATE
AS
UPDATE tabSparePart
SET Price = MD.Price
FROM tabSparePart INNER JOIN
(
SELECT inserted.[Material Number (SAP)] AS MaterialNumber, inserted.Price
FROM inserted
GROUP BY [Material Number (SAP)], inserted.Price
) MD
ON tabSparePart.SparePartName = MD.MaterialNumber
I need to group by Material-Number because there are redundant rows inserted into table tabSparePartMasterData_Temp which i'm only using to update the Sparepart-Price in tabSparePart. But i assumed that the group by would sort out the duplicates(Price is same for any duplicate).
It's possible that the inserted/updated records' MaterialNumber is not available in tabSparepart. In this case this record should be "skipped". Does the INNER JOIN takes that into account?
Try adding SET NOCOUNT ON to the trigger
This error doesn't look like a SQL error and I'm guessing the client code is getting confused by the 2nd update in the trigger.
Edit: this error can be caused by the data grid view in SSMS being silly.
This isn't a SQL message as I thought: it is an SSMS being stupid message
See these which all says "learn to write SQL"
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dealing-with-the-row-value-s-updated-or (Less than dot blog)
Trigger that modifies multiple rows on diffrent table then it was invoked on in SQL Server 2005
SSMS permits duplicate records in a table, but not subsequent updates
Saying that, there is a KB article about a bug in SQL Server 2005...