[T-SQL] Check if Function EXISTS before adding |
Check if Function EXISTS before adding Posted: 08 Jul 2013 04:43 AM PDT I'm trying to write of script that checks if a certain Function exists, and if it does not, create it. If the Function does exist, then the script should end without touching the function.I've tried writing it these ways[code="sql"]If NOT EXISTS (select * from sysobjects where name = 'FunctionName')CREATE FUNCTION --....-- andIF OBJECT_ID('FunctionName','IF') is nullCREATE FUNCTION --....[/code]I have read that CREATE FUINCTION cannot be combined with other statements in the batch and that the CREATE statement must start the batch. I've tried puting the CREATE inside BEGIN/END and parantheses. But I'm still getting syntax errors.Is the only option to use the syntax that drops the function when it already exists and create it every time? |
Posted: 08 Jul 2013 03:31 AM PDT [b]TableName:Customer[/b] ---------------------------------------------------------------------------------CustomerId CustomerName Location City District PinCode Status---------------------------------------------------------------------------------4 Pavan HitechCity Hyd WG 7687 15 Kalyan CinemaStreet Tuni EG 1234 17 MVRao LBNagar Kkd GG 123 08 MMRao Pathapeta Nzd Krishna 342 0---------------------------------------------------------------------------------[b]TableName:CustomerDetails[/b] ---------------------------------------------------------------------------------CustomerId CustomerName Location City District PinCode Status---------------------------------------------------------------------------------4 Pavan1 HitechCity Hyd WG 7687 15 Kalyan1 CinemaStreet Tuni EG 1234 17 MVRao1 LBNagar Kkd EG 123 08 MMRao1 Pathapeta Nzd Krishna 342 0---------------------------------------------------------------------------------[b]TableName:TargetActivity[/b]------------------------------------ActivityTableId ActivityTable------------------------------------ 1 Customer 2 CustomerDetails------------------------------------Based on the above tables, i need to build a query to get the below output format.[b]Required Output[/b]-----------------------------------------------------------------------------------------ActivityTableId ActivityTable ActivityTableCount ActivityTableFilterCount----------------------------------------------------------------------------------------- 1 Customer 4 2 2 CustomerDetails 4 2-----------------------------------------------------------------------------------------[b]Note:[/b] 1. ActivityTableCount is the TOTAL COUNT OF THE CORRESPONDING ACTIVITY TABLE MENTIONED IN THE COLUMN.2. ActivityTableFilterCount is the COUNT OF RECORDS FROM THE ACTIVITY TABLE WHERE THE STATUS IS 1 (Status = 1).3. There are n number of tables / records in TargetActivity table. For example, 2 tables are given.4. I need a SELECT query only to register in our tool. Stored Procedures are not allowed.Please help in writing query for the required output.Thanks |
Posted: 08 Jul 2013 01:54 AM PDT hii want to add column with no check constraintlikealter table emp with nocheckadd column1 not nulland then i need to update with default values and then i need to make table with checkhow to do it |
You are subscribed to email updates from SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8) To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
No comments:
Post a Comment