SQL cell wise trigger - sql

Can a specific cell wise trigger be created?
Or is
IF UPDATE(COLUMN) WHERE OTHER_COLUMN LIKE 'JT'
the equivalent present in SQL Server 2008?
EDIT after getting 2nd answer---
IF not UPDATE(CurrentNo) --// Wanted to do like this : where series ='JT'
return
IF not EXISTS(SELECT 'True'
FROM Inserted i
JOIN Deleted d ON i.Series = d.Series
WHERE i.Series = 'JT' AND d.Series = 'JT')
return
Seems ok right! Please comment.

No. There is no way of doing this declaratively. You would need to create a general Update trigger and put logic in it to return immediately IF NOT UPDATE (column)
If the column of interest was updated then you would query the inserted and deleted pseudo tables to allow you to process rows where your condition of interest was met.

Tiggers are specified on tables, not on rows, columns or cells. Inside the body of the trigger you will have access to the INSERTED and DELETED tables. You can join them together to deterimine which columns were changed during an update. The UPDATE() function which is available in SQL Server 2008 (as well as previous versions) is a shorthand method for determining whether a column has changed.

Related

Informix and DROP COLUMN if exists

I am trying to write "re-usable" SQL script with ADD COLUMN clause for Informix server. Normally, I can only run it once, because then the column already exists and it cannot be added again. And I cannot just add DROP column as prior statement, because it would fail during the first execution. I need something like DROP COLUMN IF EXISTS, but unlike dropping other SQL entities, it is not available for columns.
I can ask system tables whether the column is present in then DB via:
SELECT c.colname
FROM "informix".systables AS t
JOIN "informix".syscolumns AS c ON t.tabid = c.tabid
WHERE c.colname = 'col_name' and t.tabname = 'tab_name'
plus I found some proposed solutions for workarounds, but I guess they are for different SQL servers, since I cannot figure out how to put it together into valid SQL script acceptable for my Informix.
Any clues? Or is it mission impossible?
As things stand, Informix's ALTER TABLE statement has no DROP COLUMN IF EXISTS clause, nor an ADD COLUMN IF NOT EXISTS clause (nor a separate DROP COLUMN statement — though it does have a separate RENAME COLUMN statement).
You would have to wrap the logic to detect whether the column exists in a shell script and decide based on that whether to add the column. Or, alternatively, you'd have to submit the ADD operation and ignore the 'column already exists' error.

Access 2010 SQL - UPDATE query not working

I need to create a query for updating a column in a table with values taken from another table and matching a field.
These are the 2 tables:
tblMain
ID Autonumbering
Key Text
Stat1 Integer
tblStat1
ID Autonumbering
Key Text
Freq Integer
I want to UPDATE the tblMain.Stat1 column with tblStat1.Freq value on each record in which tblMain.Key = tblStat1.Key.
I tried this syntax (found somewhere as an example)
UPDATE tblMain
SET tblMain.Stat1 = tblStat1.Freq
WHERE tblMain.Key = tblStat1.Key;
This doesn't work and returns an error on the 2nd row.
After some trials I found that the correct syntax (built with the Access query generator) is this:
UPDATE (tblMaibn INNER JOIN tblStat1 ON tblMain.Key = tblStat1.Key)
SET tblMain.Stat1 = tblStat1.Freq;
In this 2nd syntax, there is no trace of the WHERE condition.
Can someone help me to understand what's wrong with the 1st syntax.
Since I'm building a new table (the join), how can it work on tblMain?
As I said, I found the wrong syntax as an example of UPDATE statement.
Thank you in advance.
Bye,
Ivano
What is happening in your first query on the 2nd row, is that Access isn't aware of what tblStat1 represents in your query.
The reason your 2nd query is working is because it uses an inner join on the relevant key. In order for SQL to be aware of what record in tblMain relates to which record in tblStat1, you need to use a join.
You can see in the generated code that it is updating your desired table, but joining onto the second table. The where condition is redundant as you're updating every record.
In 1st syntax, you can change:
UPDATE tblMain
SET tblMain.Stat1 = (SELECT Freq
FROM tblStat1
WHERE tblMain.Key = tblStat1.Key)

Update statement clarification

I'm extremely new to "updating" databases as I've only ever wrote queries as select statements.
I have a record i need to delete or change from the database. How would I go about doing that? Lets take updating a column first.
I want to update the "Customer" table and the "SNumber" column where the "TicketNum" Column is "123" Right now "SNumber" for that record is blank, and i'd like it to be "115"
I was thinking:
update Customer
set snumber = '115'
where ticketnum = '123'
Obviously I do not want to run this query with fear of messing up the database.
The second part of this is how would I delete the record all together?
Thanks
that is correct.
remember you can update and then issue a ROLLBACk if you need to undo it.
delete is similar:
delete customer where ticketnum = 123;
If you are new I recommend searching about CRUD operations using tSQL. For example:
Update on MSDN
Delete on MSDN
As for your question. You will not mess up the database with that UPDATE clause, it is correct.
To delete the row just use:
DELETE FROM Customer
WHERE ticketnum = '123'
ROLLBACK, as recommended on other answer, helps to roll back an action (transaction) that was incorrect or erroneus or just because some data is not correct. See MSDN for ROLLBACK sintax
This is closely related to TRANSACTIONS concept.
The general form of update in SQL is
UPDATE table
SET column = expression
WHERE predicates;
and an example would be
UPDATE suppliers
SET description = 'gorilla glass', product = 'screens'
WHERE name = 'corning';
As other posters have noted, your SQL update is correct.

Multiple SQL Update Statements in single query

I am in a situation where I am having to update about 12,000 items in my DB.
Each row needs to mirror an excel file that I made previously.
I have made the file that creates each line of SQL statement, but I am not sure if I can run each line in a single query.
This is an example of what I am trying to do.
UPDATE [STORESQL].[dbo].[RPT_ITM_D] SET F1301='1.29' WHERE F01='0000000000001'
UPDATE [STORESQL].[dbo].[RPT_ITM_D] SET F1301='1.39' WHERE F01='0000000000002'
Will this work, or are there any better options for what I am trying to achieve?
Each item will have a unique value and the column to be changed will have a unique value as well. I don't see how I could make this work with a loop, or any other methods I've found so far. I realize that this might take a long time to process, but time is not an issue.
Thank you in advance
Something like this is the best you can do:-
UPDATE [STORESQL].[dbo].[RPT_ITM_D]
SET F1301 =
case F01
when '0000000000001' then '1.29'
when '0000000000002' then '1.30'
ELSE F1301
end
Other than that, running multiple updates is the way to go.
Yes, you could add all the single-line-Update-statements in one query like you are doing.
Take a look at MERGE e.g. something like:
MERGE INTO [STORESQL].[dbo].[RPT_ITM_D]
USING (
VALUES ('1.29', '0000000000001'),
('1.39', '0000000000002')
) AS source (F1301, F01)
ON F01 = source.F01
WHEN MATCHED THEN
UPDATE
SET F1301 = source.F1301;
...but using a table value constructor in this way would not scale to 12,000 rows! So look to first copying the data from Excel to a table on the server, then use the table as the source e.g.
MERGE INTO [STORESQL].[dbo].[RPT_ITM_D]
USING [STORESQL].[dbo].MyStagingTable AS source
ON F01 = source.F01
WHEN MATCHED THEN
UPDATE
SET F1301 = source.F1301;
If you have a significant amount of data to update, it may be advantageous to load the excel file into the database as a table, then update your table based on the data in this loaded table.
UPDATE RPT_ITM_D
SET F1301 = NewTable.Value
FROM RPT_ITM_D INNER JOIN NewTable ON (NewTable.F01 = RPT_ITEM_D.F01);
If you're on SQL server, you could use SSIS to load the file pretty quickly.
I think the best way is to import the Excel sheet into a table in your SQL database. From there you could be able to use a join to create a single update statement for all 12,000 items.
For information on how to import Excel sheet into SQL: http://msdn.microsoft.com/en-us/library/ms141209.aspx
The update statement would then look something like this:
UPDATE itemTable
SET F1301 = excelTable.<column with your value>
FROM [STORESQL].[dbo].[RPT_ITM_D] itemTable inner join [STORESQL].[dbo].[importedExcelTableName] excelTable on itemTable.F01 = excelTable.<column with the item code>
If you aren't sure if this would work safely, you can try this query for a single value by simple adding:
WHERE itemTable.F01 = '0000000000001'
You can use the concatenate function in Excel to frame a query, All you need to do is to frame a single update query and drag the same for the rest
concatenate("Update set =",,",where =
",,";")
Use the above format and drag the cell till the end or double click on the bottom right corner for Auto fill of the Update statement. I beleive this is the shortest way possible and run it in a single Go.
Make a unique constraint on the F01 column and then use an insert statement with an 'ON DUPLICATE KEY UPDATE' statement.
INSERT INTO [STORESQL].[dbo].[RPT_ITM_D] (F01, F1301)
VALUES
('0000000000001','1.29'),
('0000000000002','1.39')
ON DUPLICATE KEY UPDATE F1301 = VALUES(F1301);

Update A multi-valued field in Access

I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.