Click here to Skip to main content
15,742,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am creating a SQL User Define function where I pass a input. Now condition is that if the input value is 1 then Create Table1 else input value is 2 then create Table2.

Can any one help me to create this.

Thanks to all
Dishi_Gupta 7-Jan-16 4:28am    
What is the so far code you have used to make the function?
Herman<T>.Instance 7-Jan-16 4:34am    
Tomas Takac 7-Jan-16 4:39am    
This won't work. You definitely cannot create table inside CASE-WHEN. If we are talking about SQL Server then there is IF-THEN-ELSE available.
Tomas Takac 7-Jan-16 4:37am    
You cannot create tables in a UDF. Perhaps you want a Stored procedure instead?
Dishi_Gupta 7-Jan-16 4:56am    
If You really want an option to create tables under conditions, instead of making two different tables create one table only, but specifying different column names for each case, assuming that the values are later stored in separate tables..

1 solution

You can't do it.
Look at the documentation:[^] and you will see that:
The statements in a BEGIN...END block cannot have any side effects. Function side effects are any permanent changes to the state of a resource that has a scope outside the function such as a modification to a database table. The only changes that can be made by the statements in the function are changes to objects local to the function, such as local cursors or variables. Modifications to database tables, operations on cursors that are not local to the function, sending e-mail, attempting a catalog modification, and generating a result set that is returned to the user are examples of actions that cannot be performed in a function.

You can do these in a stored procedure, (but you can't call them from a UDF either) but you cannot CREATE, ALTER, or DROP tables within a function.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900