2009/09/14

SQL - check if primary key exists in a table

IF EXITS (SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TABLE_NAME = '..... your table name ....'
AND TABLE_SCHEMA ='dbo' )
BEGIN

---- Your code here

SQL - create stored procedure if not exists

IF NOT EXISTS(Select * from sysobjects where name = 'usp_UserSproc')
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE usp_UserSproc
(
@ID int = NULL --nullable input
)
AS
BEGIN
-- BLAH BLAH BLAH
END';
END