Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,
am creating a temporary table in my application
am checking the table first and creating it with a sp
SQL
create proc [dbo].[temp]
as

Begin
IF EXISTS
(
SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N'tempdb..##Mytemp')

)

DROP TABLE ##Mytemp
ELSE

CREATE TABLE ##Mytemp(Col1 binary(100), Col2 binary)
End


i need to add columns of binary datatype based on a value
if the value is greatethan 3(i.e 4 ) then it should add column like
col+ maxnumber

below is my sample sp
SQL
create proc [dbo].[temp1]
as

Begin
DECLARE @ColName nvarchar(100)
DECLARE @DynamicSQL nvarchar(250)
SET @ColName='col4'
SET @DynamicSQL = 'ALTER TABLE ##Mytemp ADD ['+ CAST(@ColName AS nvarchar(100)) +'] nvarchar(100) NULL'
EXEC(@DynamicSQL)
End


this sp will create 1 col nedd some more correction in my script plz give me any suggestion
thanks in advance :)
Posted
Updated 18-Nov-11 0:56am
v3
Comments
Mehdi Gholam 18-Nov-11 4:54am    
EDIT -> fixed formatting
Pandya Anil 18-Nov-11 6:59am    
what you want solution or suggestion over your given solution ?
RDBurmon 12-Dec-11 6:44am    
Have you encountered with any error related with your queries ? Please post here if any

 
Share this answer
 
SQL
SELECT count(COLUMN_NAME) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'LOGINTABLE'

create proc [dbo].[temp1]
as

Begin
DECLARE @ColName nvarchar(100)
DECLARE @DynamicSQL nvarchar(250)
DECLARE @pTOTALCOLUMN INT
SELECT @pTOTALCOLUMN=count(COLUMN_NAME) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = '##Mytemp'
SET @ColName='COL' + cast(@pTOTALCOLUMN as varchar(10))
SET @DynamicSQL = 'ALTER TABLE ##Mytemp ADD ['+ CAST(@ColName AS nvarchar(100)) +'] nvarchar(100) NULL'
EXEC(@DynamicSQL)
End
 
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