i want to create procedure for create table in sql server
2005
for example
create procedure create_table
@table varchar(20)
as
create @table(
id int,
name char(20)
)
but it will get error
what is solution?
Answers were Sorted based on User's Feedback
Answer / saravanan sankar
CREATE PROCEDURE create_table
@table varchar(20)
AS
BEGIN
declare @string varchar(5000)
set @string = 'CREATE TABLE '+ @table +'(id int,name char(20))'
END
exec(@string)
//FIRST RUN PROCEDURE ABOVE AND THEN EXECUTES
EXEC create_table 'TABLE1'
Is This Answer Correct ? | 8 Yes | 1 No |
Answer / pradip jain
create procedure create_table
@table1 varchar(20)
as
Begin
Declare @table table
(
id int,
name char(20)
)
end
If you refer question only syntax error is there!!! as table
variable can not use using create command.
Is This Answer Correct ? | 2 Yes | 0 No |
Here is the solution...
create proc CREATE_TABLE
@TableName varchar(50)
as
begin
declare @String nvarchar(max)
set @String='create table '+@TableName +'(ID int,Name
varchar(50))'
execute sp_executesql @String
end
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / patan
IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'PROCEDURENAME')
DROP PROCEDURE PROCEDURENAME
GO
CREATE PROCEDURE PROCEDURENAME
@TEST INT = 0
AS
BEGIN
--HERE CREATING TABLE
DECLARE @CREATE TABLE (ID INT ,NAME VARCHAR(20))
INSERT INTO @CREATE VALUES (1,'PATAN')
.
.
.
.
.
.
END
IN PROCEDURE WE CAN CREATE TABLE AND IT CAN USE THE OUR PROCEDURE @CREATE IS THE TABLE NAME
NOTE:WHILE RUNNING TIME WE CAN CREATE TABLE WITH THERE ALIES NAMES...
THANKS @ GOOD LUCK
Is This Answer Correct ? | 0 Yes | 0 No |
How to find the date and time of last updated table?
What is Sqlpaging in SqlServer 2005 ?
What are different types of Keys? Please explain all the keys with a suitable example.
What is extent? Types of extents?
Write a sql query to delete duplicate records from a table called table1
can foreign key take role of primary key?
5 Answers CarrizalSoft Technologies, TCS, Villa Marie,
What is a View ? Can we insert, Update and delete a view?
What are sql server functions?
WHAT IS THE DIFFERENCE BETWEEN CANDIDATE KEY ,COMPOSITE KEY AND PRIMARY KEY. THAN U.
What is nested transaction?
Describe the left outer join & right outer join. : sql server database administration
How to make a column nullable?