I have AFTER UPDATE trigger on table.
I need to get the name of changing colomn and it's old and new values.
To do statement UPDATE(column_name) with each column in code - bad solution. But I can't get all table colomns names via query
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'smth'
and in cursor dynamically get UPDATE(#column_name) value.
At same time when I try to use COLUMNS_UPDATED() function when I update only one column in table, I have results (value converted to int):
64 (Updated column with ORDINAL_POSITION = 31)
32 (Updated column with ORDINAL_POSITION = 30)
8 (Updated column with ORDINAL_POSITION = 29)
4 (Updated column with ORDINAL_POSITION = 28)
2 (Updated column with ORDINAL_POSITION = 27)
1 (Updated column with ORDINAL_POSITION = 26)
32768 (Updated column with ORDINAL_POSITION = 25)
I think it is very strange and ask your help.
Whether it is strange or not, this is at least documented:
Caution
In SQL Server 2008, the ORDINAL_POSITION column of the INFORMATION_SCHEMA.COLUMNS view is not compatible with the bit pattern of columns returned by COLUMNS_UPDATED. To obtain a bit pattern compatible with COLUMNS_UPDATED, reference the ColumnID property of the COLUMNPROPERTY system function when you query the INFORMATION_SCHEMA.COLUMNS view, as shown in the following example.
SELECT TABLE_NAME, COLUMN_NAME,
COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),
COLUMN_NAME, 'ColumnID') AS COLUMN_ID
FROM AdventureWorks2008R2.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Contact';
UPDATE() will return true if the column is referenced, it doesn't matter if the value hasn't changed
so if you do
update table BLa
set Col1 = Col1
in the trigger UPDATE() will return true for that column
join inserted and deleted pseudo-tables in the trigger and check that the values haven't changed also make sure to account for NULLs of course....
Related
I've been using this simple query as a way to find data when I'm working on new queries for my coworkers. Is it possible to upgrade it to return a 4th and 5th column that reports how many rows are in the Table and how many NULL values are in the column? That would save me so much time during my exploration.
SELECT
TABLE_SCHEMA AS 'SchemaName'
,TABLE_NAME AS 'TableName'
,COLUMN_NAME AS 'ColumnName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%cost%'
AND TABLE_SCHEMA = 'dbo'
ORDER BY TableName, ColumnName;
I'm having difficulty with a simple issue.
I have a number column a typical value is say 192.123.
I want to add/subtract a single value to it, say -100.123.
I assume it is
Update table_name
SET column_name = (the original value - 100.123)
How should I go about this?
You need to use '+' instead of '-'. So that it will add or subtract based on the input value.
Update table_name SET column_name = (column_name + Value_to_add_subtract)
use the column_name in original_value
Update table_name SET column_name = (column_name - 100.123)
use columnName instead of original value
update table_name
SET column_name = (column_name - 100.123)
Is it possible to display just certain table names in the query:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
Most of the time I would need all the table names but there are curtain situations where I need just certain row names.
When I try doing the following:
USE [WebContact]
SELECT COLUMN_NAME, ContactDateTime
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
It tell me that
Invalid column name 'ContactDateTime'
even though that is one of the row names.
Is this possible to do?
The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view.
Since it is not a column there, SQL Server is going to error out saying that it is invalid.
I think what you're trying to do is add another WHERE clause to your statement:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] = 'ContactDateTime'; -- Here!
Or if you want to add multiple columns...
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME]
IN ('ContactDateTime', 'column2', 'column3', ... 'column(n)'); -- Here!
Also see here for the case against using INFORMATION_SCHEMAS.
if ContactDateTime is a column that you are looking for in table memberEmails then you can do this
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
and COLUMN_NAME='ContactDateTime'
The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime. Please read document first
So I have this:
CREATE PROCEDURE getBattingColumnNames
AS
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Batting'
AND COLUMN_NAME NOT IN ('playerID','yearID','stint','teamID','lgID', 'G_batting','GIDP','G_old');
GO
And it works great. I get all the column names that I want, in c# I use this to populate a drop down with the column names. however, one of my column names, "Doub" I would like to change. So playing around with it I tried:
SELECT COLUMN_NAME.Doub AS 'DB'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = 'Batting')
and a variation of that, and the error is the mulitplart identifier could not be bound. How can i change that column name in this query?
You could use a case to translate the column name:
select case COLUMN_NAME
when 'Doub' then 'DB'
else COLUMN_NAME
end
from ...
I want to change the type of a column in MS SQL Server 2005, but before i change the type of that column i want to check if that column is of the type i want to change. How can i do that in SQL?
Thanxs,
Bas Hendriks.
Based on the anwser i wrote the following query that did the trick:
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'e_application'
AND COLUMN_NAME = 'txt_locked_by'
AND DATA_TYPE = 'nvarchar'
AND CHARACTER_MAXIMUM_LENGTH = 15 )
BEGIN
ALTER TABLE.....
END
You can query the INFORMATION_SCHEMA tables.
E.g.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable'
you can use sp_help TableName , it also retrieve's the column's data types and other properties