Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
This procedure gives me the number of columns and column names I want to build a table created those columns

SQL
ALTER PROCEDURE [dbo].[GetCountOfColumnToTable]
	@DBNAME NVARCHAR(50),@TBLNAME NVARCHAR(50)
AS
BEGIN
	
declare @TotalColumns int =
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = @DBNAME AND TABLE_SCHEMA = 'dbo' AND TABLE_NAME = @TBLNAME)
declare @CountColumns int =@TotalColumns
WHILE @CountColumns <= @TotalColumns
    BEGIN
	SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = @DBNAME AND TABLE_SCHEMA = 'dbo' AND TABLE_NAME = @TBLNAME
	SET @CountColumns = @CountColumns + 1
	END

END
Posted
Comments
ChauhanAjay 18-Sep-14 23:23pm    
You want a exact replica of Table1 with a new name Table2?
Do you also want the data from Table1 to Table2?

 
Share this answer
 
The question is not clear explain the question; what do you want to achieve using this SP.
 
Share this answer
 
Comments
alhlwany.it 18-Sep-14 4:51am    
this proc give me A column name i need used this column name to creat another table

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