I have a column in my DB which is currently defined as NOT NULL.
I would like to update this column to allow NULLs.
I have the following script to do this however I would like to check first if the column is already NULL (or NOT NULL), as it may have been changed previously.
ALTER TABLE [dbo].[aud]
ALTER COLUMN [actname] nvarchar(50) NULL
Any help appreciated.
Use COLUMNPROPERTY to get column property . You may write something like
SELECT COLUMNPROPERTY(OBJECT_ID('dbo.aud'),'actname','AllowsNull') AS 'AllowsNull';
For more information please visit this link
select is_nullable from sys.columns c inner join sys.tables t on
t.object_id = c.object_id where t.name = 'aud' and c.name = 'actname'
Will give you a BIT representing whether it is nullable or not.
So you could switch on this like
IF EXISTS(SELECT * from sys.columns c inner join sys.tables t on
t.object_id = c.object_id where t.name = 'aud' and c.name = 'actname' AND
is_nullable = 1)
BEGIN
--What to do if nullable
END
ELSE
BEGIN
--What to do if not nullable
END
END
That of course assumes that the table and column exist at all...
There isn't really a need to do that, because if it's already Nullable, changing a column from Nullable to Nullable will have no negative effect.
However you can do it with this query:
SELECT is_nullable
FROM sys.columns
WHERE object_id=object_id('YourTable') AND name = 'yourColumn'
Related
The following is my code
IF NOT EXISTS (SELECT 1 FROM sys.objects o INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE
o.name = 'portfolioAttributeCodes'
AND c.name = 'isDisplayed'
)
BEGIN
ALTER TABLE
[cosmos].[portfolioAttributeCodes]
ADD
[isDisplayed] bit DEFAULT 1;
END
IF EXISTS (SELECT 1 FROM sys.objects o INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE
o.name = 'portfolioAttributeCodes'
AND c.name = 'isDisplayed')
BEGIN
UPDATE [cosmos].[portfolioAttributeCodes] SET [isDisplayed] = 1;
END
Now what is happening is it will not create a column (skips the first Id statement and gets into the second one and fails with Invalid column name 'isDisplayed'
Can some one help?
If the table doesn't have the column isDisplayed already the entire batch will fail, as the parser will generate an Invalid column name error. This occurs before any of the SQL is run, so it's not that the second IF is being entered, none of the SQL is run at all. It's effectively a compilation error (like when you try to build you C# application and you have a reference to an object you haven't defined).
You cannot reference a new column in the same scope it was created. You would need to use 2 batches or put the reference to the column in a separate scope, so that its validation is deferred.
A deferred validation would seem fine here:
IF NOT EXISTS (SELECT 1
FROM sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE o.name = 'portfolioAttributeCodes'
AND c.name = 'isDisplayed')
BEGIN
ALTER TABLE [cosmos].[portfolioAttributeCodes]
ADD [isDisplayed] bit CONSTRAINT DF_isDisplayed DEFAULT 1 WITH VALUES;
END;
ELSE
BEGIN
EXEC sys.sp_executesql N'UPDATE [cosmos].[portfolioAttributeCodes] SET [isDisplayed] = 1;';
END;
I also switch to an ELSE as there is little point updating the column after you've created it; just create the column with the values in the first place. I name the DEFAULT CONSTRAINT as well as that's just good habit.
I need something that is the logical equivalent of
select cast(* as numeric IF data_type='decimal') from table_name
I didn't know how to do that, so i'm trying to reduce it into two steps:
select * from table_name where data_type!='decimal'
select cast(* as numeric) from table_name where data_type='decimal'
But I realized I don't know how to do that, either. I can find the list of column names that are decimal thanks to SO, but as this is a batched process, i can't split this into two steps
Is what I'm trying to do possible? I just want to get a set of columns, but if they're decimal, cast them as double.
Thanks!
You have to explicitly cast each column like this:
SELECT CAST(Col1 AS INT),CAST(Col2 AS INT)..
FROM Table
Or, you could create a function as follows (credit goes to poster listed here):
/****** Object: UserDefinedFunction [dbo].[udf_getColumnDataType] Script Date: 8/4/2014 10:59:23 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/***************************************************************************
Code stolen from here:
http://bit.ly/1v26PmM
****************************************************************************/
CREATE FUNCTION [dbo].[udf_getColumnDataType] (#TableName varchar(64), #ColumnName varchar(64) )
RETURNS varchar(64)
AS
BEGIN
DECLARE #DataType VARCHAR(10)
SELECT #DataType =
t.Name --'Data type'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
c.object_id = OBJECT_ID(#TableName)
and c.name = #ColumnName
return #DataType
END
You then can check each column and cast accordingly. Just pass the table name and the column name (as string) and it will return it to you.
Select iif(
dbo.udf_getColumnDataType('your table name', 'your column name') ='decimal',
cast(your_column_name as numeric),
your_column_name
) as col1
from your_table_name
I was trying to create this on SQL Fiddle, but seems like I can't create functions on their page. It worked for me while testing in SQL Server 2012. Hopefully, it's not a long route for you, but worth posting.
I am using SQL server 2008. I need to find if default value constraint does not exist then create it. Here is what I have tried.
IF (NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='MyConstraint'))
BEGIN
ALTER TABLE [XX] ADD CONSTRAINT [MyConstraint] DEFAULT ((-1)) FOR [XXXX]
END
GO
if not exists (
select *
from sys.all_columns c
join sys.tables t on t.object_id = c.object_id
join sys.schemas s on s.schema_id = t.schema_id
join sys.default_constraints d on c.default_object_id = d.object_id
where t.name = 'table'
and c.name = 'column'
and s.name = 'schema')
....
I find this to be easier:
IF OBJECT_ID('SchemaName.MyConstraint', 'D') IS NULL
BEGIN
-- create it here
END
I was a bit puzzled as to why this simple task was so complicated. In my case, I don't have constraint names - only table and column names. I want to check if they already have a default before trying to add one.
After a bit more digging, I came up with this:
IF (SELECT Column_Default FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MY_TABLE' AND COLUMN_NAME = 'MY_COLUMN') is NULL
BEGIN
ALTER TABLE [dbo].[MY_TABLE]
ADD DEFAULT ('') FOR [MY_COLUMN]
END
GO
I have to implement this in a ginormous boilerplate script, so the shorter the better.
if not exists(select 1 from sys.default_constraints where name = 'SchemaName.MyConstraint')
begin
-- create it here
end
The following works for me on SQL Server 2016.
Assuming I have a table named MY_TABLE and a column MY_COLIUMN.
I would like to add a constrain (default to '-1' ) on MY_COLIUMN that need to add the constrain on.
/* Test for the specific column */
IF EXISTS (select 1 from sys.all_columns c where c.object_id= OBJECT_ID(N'MY_TABLE') and c.name='MY_COLIUMN')
BEGIN
/* Add default if not exits */
IF NOT EXISTS (
select 1 from sys.default_constraints c where c.object_id =
(
select default_object_id from sys.all_columns c where c.object_id = OBJECT_ID(N'MY_TABLE') and c.name='MY_COLIUMN'
)
)
BEGIN
ALTER TABLE MY_TABLE
ADD DEFAULT '-1' FOR MY_COLIUMN;
END
END
GO
I've used the following in the past:
DECLARE #default sysname
SELECT #default = object_name( cdefault ) FROM syscolumns WHERE id = object_id( 'DBO.TABLE' ) AND name = 'COLUMN'
IF ( not #default is null )
BEGIN
...
END
Search the system table where the default costraints of the database are stored, without the schema name:
IF EXISTS(SELECT 1 FROM sys.default_constraints WHERE [name] = 'MyConstraint')
print 'Costraint exists!';
ELSE
print 'Costraint doesn''t exist!';
I know I'm coming to the party late here, but I am a big fan of OBJECTPROPERTY. Here is how to set a default of 1 on a column if the default does not yet exist.
IF (OBJECTPROPERTY(OBJECT_ID('My_constraint_name'),'CnstIsColumn') IS NULL
ALTER TABLE Mytable ADD CONSTRAINT [MY_constraint_name] DEFAULT ((1)) FOR [My_column_name]
I've got really weird logic where I getting my custom table with custom nullable fields.
How can I change all NULL values in the table to 0 ?
Sorry but with custom fields I mean that
I don't know the name of columns !
UPDATE custom_table
SET the_column = 0
WHERE the_column IS NULL;
By querying sys.columns and sys.objects you can generate a query that alters every column to a default value (specified by you) for the column types you want to set a default non-null value for.
Something like this will give you something to look at:
SELECT so.name,
sc.name AS varname ,
st.name AS typename ,
sc.max_length ,
sc.[precision] ,
sc.scale ,
sc.collation_name
FROM sys.columns sc
JOIN SYS.types st ON sc.system_type_id = st.system_type_id
AND sc.user_type_id = st.user_type_id
JOIN sys.objects so ON so.object_id = sc.object_id
You can then attach a cursor for this and run a series of alter/update commands based on typename (column type) and varname (column name).
If you mean in ALL columns, then you could do a very expensive query that will touch all records and columns:
UPDATE mytable
SET
col1 = isnull(col1, 0),
col2 = isnull(col2, 0),
col3 = isnull(col3, 0),
col4 = isnull(col4, 0),
col5 = isnull(col5, 0)
-- etc all the numeric columns *
If you included date columns, they go to '1900-01-01'
UPDATE mytable SET mycol=0 WHERE mycol IS NULL
I want to find out whether the table has an identity column or not. Table is unknown to me. I have not done the structure of the table. Using Query?
I am using Sql Server Compact Edition.
IF (OBJECTPROPERTY(OBJECT_ID('TABLE_NAME'), 'TableHasIdentity') = 1)
ObjectProperty is available starting sql server 2008 Reference:
OBJECTPROPERTY
This query returns a table's identity column name:
CREATE PROCEDURE dbo.usp_GetIdentity
#schemaname nvarchar(128) = 'dbo'
,#tablename nvarchar(128)
AS
BEGIN
SELECT OBJECT_NAME(OBJECT_ID) AS TABLENAME,
NAME AS COLUMNNAME,
SEED_VALUE,
INCREMENT_VALUE,
LAST_VALUE,
IS_NOT_FOR_REPLICATION
FROM SYS.IDENTITY_COLUMNS
WHERE OBJECT_NAME(OBJECT_ID) = #tablename
AND OBJECT_SCHEMA_NAME(object_id) = #schemaname
END
Then form the code side.
Call this stored procedure using the datareader role, then check datareader.hasrows(). If the condition value is true (1), then the table has identity column if set. If not then it doesn't have an identity column.
I know it's long time ago but i found this helpful
try this :
IF EXISTS (SELECT * from syscolumns where id = Object_ID(#TABLE_NAME) and colstat & 1 = 1)
BEGIN
-- Do your things
END
Any of the below queries can be used to check if an Identity Column is present in the table
1)
SELECT *
FROM sys.identity_columns
WHERE OBJECT_NAME(object_id) = 'TableName'
2)
SELECT *
FROM sys.identity_columns
WHERE object_id = (
SELECT id
FROM sysobjects
WHERE name = 'TableName'
)
I would just like to add this option as well as I think it is the simplest
SELECT COLUMNPROPERTY(OBJECT_ID('TableName'),'ColumnName','isidentity')
One way to do this would be to make use of the stored procedure sp_help. I.e:
sp_help MyTable
This will return a DataSet that has all the information you would need on the table. There is a specific Table that has information on identities.
I.e:
If it does not contain an identity field, the Identity column will say: "No identity column defined".
#Pranay: he said Compact Edition. Stored procedures aren't supported, and there is no sys.anything.
This is the call:
SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE AUTOINC_INCREMENT IS NOT NULL AND TABLE_NAME='this_table'
It will return either 1 (true) or 0 (false).
...
declare #tblhasIdentCol bit = IF (IDENT_CURRENT( #dbName +'.'+ #schemaName +'.'+ #tableName ) IS NOT NULL , 1 , 0 )
You get numeric value if table has identity
Very simple answer would be to run this:
SELECT IDENT_CURRENT('TABLE-NAME')
This would give max value of identity column if exists, if the column doesn't exist, it gives 1 as result.
Based on max value, you can identify which column is having that and determine the identity column.
This the query that get u all the tableNames, columnnames of the table, and is_identity or not in the selected database
SELECT
sys.columns.name
, sys.tables.name
, is_identity
FROM sys.columns
INNER JOIN sys.tables ON sys.tables.object_id = sys.columns.object_id
AND sys.columns.is_identity = 1
CREATE FUNCTION dbo.fnTableHasIdentity(#Tbl sysname)
RETURNS TINYINT
BEGIN
RETURN OBJECTPROPERTY(OBJECT_ID(#Tbl), 'TableHasIdentity')
END
--As simple as that!
select t.name as TableName,c.name as ColumnName
from sys.identity_columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name = 'TableName'
If you like me, needed to be able to do this for tables in an arbitrary database, then I found the following solution:
IF EXISTS (
SELECT 1
FROM [database name].sys.identity_columns AS id_col
INNER JOIN [database name].sys.objects
ON objects.object_id = id_col.object_id
INNER JOIN [database name].sys.schemas
ON schemas.schema_id = objects.schema_id
AND schemas.name = 'schema name'
WHERE OBJECT_NAME(id_col.object_id, DB_ID('database name')) = 'table name'
) SELECT 1 ELSE SELECT 0
you can get the 1 or 0 Boolean Form if the current table has identity Columns by using this
SELECT Count(Column_ID) FROM sys.identity_columns WHERE OBJECT_NAME(object_id) = 'tableName'
One way to list all Tables with their identity column if it exists
to get you desired table add at the end of the filter "and o.name='TableName'"
where Tbale Nam is the table you are looking for
SELECT o.[Name][TableName],i.[name][IdentityColName] FROM
sys.objects o
left outer join sys.identity_columns i on i.object_id=o.object_id
where o.type='U'