Drop table if exist with similar name in two schema - sql

I use this command to drop a table in sql-server 2008
IF EXISTS(SELECT name FROM [DBName]..sysobjects WHERE name = N'TableName' AND xtype='U')
DROP TABLE [DBName].[SchemaName].[TableName];
But now I have 2 tables with same name in different schema:
[DBName].[Schema1].[Members]
And
[DBName].[Schema2].[Members]
So, what is your suggestion for check if exist this tables? How can I check table names with schema?
UPDATE:
OK, there is 3 different answers and all of them worked, so I don't know which one is better, does any one know about use object_id or sys.tables?

IF EXISTS(
SELECT *
FROM [DBName].sys.tables t
JOIN [DBName].sys.schemas s
ON t.SCHEMA_ID = s.schema_id
WHERE
t.name = N'TableName' AND t.type='U'
AND s.NAME = 'SchemaName'
)
DROP TABLE [DBName].[SchemaName].[TableName];
Update:
object_id in sys.tables is the same as object_id in sysobjects for the same table. And is completely the same as function OBJECT_ID returns for the same table name. See the following illustrating examples.
So, you may simplify the query:
IF exists
(
SELECT *
FROM DBName.sys.tables
WHERE object_id = OBJECT_ID('[DBName].[SchemaName].[TableName]')
AND type = 'U'
)
DROP TABLE [DBName].[SchemaName].[TableName];
or in this way:
IF exists
(
SELECT *
FROM DBName.sys.objects
WHERE object_id = OBJECT_ID('[DBName].[SchemaName].[TableName]')
AND type = 'U'
)
DROP TABLE [DBName].[SchemaName].[TableName];
or for sql2000-styled tables:
IF exists
(
SELECT *
FROM DBName..sysobjects
WHERE object_id = OBJECT_ID('[DBName].[SchemaName].[TableName]')
AND xtype = 'U'
)
DROP TABLE [DBName].[SchemaName].[TableName];

Use this:
IF EXISTS
(
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[DBName].[Schema1].[Member]')
AND type in (N'U')
)
PRINT 'Yes'
ELSE
PRINT 'No';

Don't use sysobjects. Use the modern system views in the sys schema (introduced in 2005):
select * from sys.tables
where
schema_id = SCHEMA_ID('Schema1') and
name='tablename'
As soon as you have one "modern" schema in a 2005 or later database, you cannot reliably use sysobjects to match with schemas. If you only have "old" schemas (objects belonging to users and roles), you may be able to query based on user_id.

Wouldn't it be simplest just to:
IF object_id('[schema].[table]') > 0
DROP TABLE [schema].[table]
For non existent tables object_id() returns NULL
For some system tables it returns a negative int

Related

How to do an IF EXISTS in hana SQL query

I have this query code:
IF EXISTS (
SELECT
*
FROM sys.objects
WHERE object_id =
OBJECT_ID(N'[dbo].[BAL_WMS_STAGING]')
AND type in (N'U')
)
DROP TABLE
[dbo].[BAL_WMS_STAGING]
GO
CREATE TABLE
I am trying to use this statement to query an SAP Hana Database. The beginning of the query IF EXISTS keeps throwing an error, I assume that this means that this keyword does not exist in Hana SQL syntax. Does anyone know how to do an IF EXISTS check in Hana, as I am unable to find any answer online.
If EXISTS is a non-standard syntax that HANA doesn't support.
As an alternative you can query the catalog and write a IF...THEN statement in SQLScript.
An alternative to using IF EXISTS could be something like this:
SELECT *
FROM sys.objects
WHERE object_id = (
CASE
WHEN EXISTS (
SELECT object_id
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[BAL_WMS_STAGING]')
AND type IN (N'U'))
THEN (
SELECT object_id
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[BAL_WMS_STAGING]')
AND type IN (N'U'))
ELSE
NULL
END
)
AND type IN (N'U')
SQLScript does not have IF EXIST condition.
But same logic can be achieved by creating a variable (flag) that is initialized by your first query.
You dont need to select all the columns. You can select as an example object_id.
flag = '';
SELECT object_id INTO flag FROM sys.objects
WHERE object_id =
OBJECT_ID(N'[dbo].[BAL_WMS_STAGING]')
AND type in (N'U');
IF flag != '' THEN
DROP TABLE [dbo].[BAL_WMS_STAGING]
END IF;

Drop indexes or alter

I am trying to drop pk constraint and drop index on the target table in informatica, these below statements are working first time successfully.
IF EXISTS (SELECT Name FROM sysindexes WHERE Name = 'xyz')
DROP INDEX [xyz] ON [dbo].[Table_Name];
IF EXISTS (SELECT Name FROM sysindexes WHERE Name = 'xyz')
ALTER TABLE [dbo].[Table_Name] DROP CONSTRAINT [xyz];
But if I run the same query a second time it is giving an error:
Cannot drop the index 'dbo.Table_Name.xyz', because it does not exist or you do not have permission
I need an If ... Else statement syntax like if exists drop else end or success something.
The likely cause for this is that you might have more than one index named xyz on your database. Your IF EXISTS SELECT statement is not taking into consideration which table xyz is on.
You can check for this condition yourself by running the select statement by itself.
Try changing your query to the following to limit the scope:
If Exists
(
Select *
From sys.indexes
Where name = 'xyz'
And Object_Id = Object_Id('dbo.Table_Name')
)
Drop Index xyz On dbo.Table_Name;
One way to get around this issue is to trick the parser:
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE SCHEMA_NAME = 'dbo' AND TABLE_NAME = 'Table_Name' AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME = 'xyz')
BEGIN
EXEC('ALTER TABLE [dbo].[Table_Name] DROP CONSTRAINT [xyz]')
END

How to list all columns in views with relations - SQL Server

I made a lot of views with 7-20 columns in each of them. I need to make a query which will return me the list of tables and columns on which is related every column in every view.
Example of my view:
CREATE VIEW example AS
SELECT t.NAME, t.AGE, t.ADDRESS, p.MOBILE, p.LAPTOP ...
FROM person t, device p
WHERE ...
Query result (needed):
TABLE COLUMN
person NAME
person AGE
person ADDRESS
device MOBILE
device LAPTOP
Is this possible, and how? It saves me a lot of time (there is over 900 columns in all views).
Thanks
Yet you can have this query to pull all the views present in your database along with columns present in them views.
SELECT v.name AS View_Name
,c.name AS Column_Name
FROM sys.views v
INNER JOIN sys.all_columns c ON v.object_id = c.object_id
Here is a more elaborated solution, the idea is to convert views to temp tables with no records (select top 0 * from ...) and for each table retrieve the column names from INFORMATION SCHEMA:
---Create this table First
CREATE TABLE dbo.Views_Columns(
id INT IDENTITY(1,1) PRIMARY KEY,
ViewName varchar(100),
ColumnName varchar(100)
)
--START
DECLARE #CurrentView varchar(100)
DECLARE #CurrentSchema varchar(50)
--Temp table #AllViews stores all views names
SELECT s.name as SchemaName, v.name as ViewName, 0 as Processed
INTO #AllViews
FROM sys.views v INNER JOIN SYS.schemas s ON v.schema_id = s.schema_id
WHILE EXISTS (select * from #AllViews WHERE Processed = 0)
BEGIN
--Clean up our temp table
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'temp')
BEGIN
DROP TABLE dbo.temp
END
SELECT TOP 1 #CurrentView = ViewName, #CurrentSchema = SchemaName
FROM #AllViews WHERE Processed = 0
EXEC('SELECT TOP 0 * INTO dbo.temp FROM '+#CurrentSchema+'.'+#CurrentView)
INSERT INTO Views_Columns
SELECT #CurrentSchema+'.'+#CurrentView, Column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'temp'
UPDATE #AllViews SET Processed = 1 where ViewName = #CurrentView
END
SELECT * FROM Views_Columns
--END

Is it possible for an SQL table to have zero columns?

I'm using the following query to gather information about a table's columns:
SELECT COLUMN_NAME,
ORDINAL_POSITION,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
Is_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName'
ORDER BY ORDINAL_POSITION
If this query returns zero results, can I safety declare that the table doesn't exist? Or is is somehow possible that the table exists but (perversely) has no columns?
I'm already querying INFORMATION_SCHEMA.TABLES to find out if the table exists, but I'd like to cut it down to one query if possible.
For future reference, I found these related questions:
Create a table without columns
Can I select 0 columns in SQL Server?
If you try:
create table TestTable (id int)
alter table TestTable drop column id
SQL Server complains that:
Msg 4923, Level 16, State 1, Line 1
ALTER TABLE DROP COLUMN failed because 'id' is the only data column in table
'TestTable'. A table must have at least one data column.
So a table must always have at least one column.
If you were to look at the definition for the INFORMATION_SCHEMA.COLUMNS view, you'd see that it starts with the sys.objects table, looking for types of 'U' (Table, user-defined) or 'V' (view), so it is already performing the check for table existence for you.
CREATE VIEW INFORMATION_SCHEMA.COLUMNS
AS
SELECT
...
FROM
sys.objects o JOIN sys.columns c ON c.object_id = o.object_id
LEFT JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE
o.type IN ('U', 'V')

How to identify whether the table has identity column

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'