| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | yvartanStarting Member
 
 
                                        1 Post | 
                                            
                                            |  Posted - 2014-10-06 : 13:07:30 
 |  
                                            | I am having a problem adding new column to a table via SQLCMD here is the code ---------------------------------------------------------------------:r ServerParam.sql:connect $(sqlServer) -U $(sqlUser) -P $(sqlPass)DECLARE @cmd varchar(5000)DECLARE @SServer varchar(1000)DECLARE @database varchar(1000)DECLARE @tableName varchar(500)set @database='CalJobsErrors'SET @SServer = '$(inputSrc)'SET @tableName = '$(subgt)'+'_'+'$(tblName)'PRINT '------------------------------------------------'PRINT ' Start processing Table: ' + @database + '.dbo.' + @tableNamePRINT '------------------------------------------------'PRINT '                                                 'IF NOT EXISTS ( SELECT [name] FROM sys.databases WHERE [name] = @database)BEGIN	PRINT '   Creatin database: ' + @database	SET @cmd = 'CREATE DATABASE ' + @database	exec(@cmd)ENDSET DATEFORMAT MDYIF object_id(@database + '.dbo.' + + @tableName) is nullBEGINPRINT '   Creating Table: '+ @database + '.dbo.' + @tableNameSET @cmd = 'CREATE TABLE ' + @database + '.dbo.' + @tableName +	'(	Column0 		varchar(50) null,	Column1 		varchar(50) null,	Column2 		varchar(50) null,	Column3 		varchar(50) null,	Column4 		varchar(50) null,	Column5 		varchar(50) null,	Column6 		varchar(50) null,	Column7 		varchar(50) null,	Column8 		varchar(50) null,	Column9 		varchar(50) null,	Column10 		varchar(50) null,	Column11		varchar(50) null,	Column12		varchar(500) null,	)'EXEC(@cmd)ENDELSE BEGIN	PRINT '   Deleting Table: '+ @database + '.dbo.' + @tableName	SET @cmd = 'DELETE FROM ' + @database + '.dbo.' + @tableName	EXEC(@cmd)ENDBEGINPRINT '   Inserting/Refreshing Data in Table: '+ @database + '.dbo.' + @tableNameSET @cmd = 'BULK INSERT ' +  @database + '.dbo.' + @tableName + '	FROM ' + char(39) + @SServer + '$(subgt)' + '\ErrorFile\' + '$(txtFile)' + char(39) +	' WITH 	( 	CODEPAGE=''RAW'',	DATAFILETYPE=''char'',	FIELDTERMINATOR=''|'', 	ROWTERMINATOR = '''+char(10)+'''	)'EXEC(@cmd)ENDBEGIN	SET @cmd = 'ALTER TABLE ' +  @database + '.dbo.' + @tableName + '			ADD (LoadDate varchar(8) null,			     processDate datetime null)'      	PRINT @cmd	exec @cmdEND---------------------------------------------------------------------when I am running this script, it creates the database and the table but when I am trying to add the last to field vi ALTER TAABLE    ADDI am getting error----------------------------------------------------------------------C:\CalJobs\SQLCMD\SQLScripts>SQLCMD -v subgt=SJI txtFile=LTS_ERROR_10022014.TXTtblName=LTS_ERROR_10022014 -i CreateErrorTableSQL.sqlSqlcmd: Successfully connected to server 'VULCAN'.------------------------------------------------ Start processing Table: CalJobsErrors.dbo.SJI_LTS_ERROR_10022014------------------------------------------------   Deleting Table: CalJobsErrors.dbo.SJI_LTS_ERROR_10022014(88 rows affected)   Inserting/Refreshing Data in Table: CalJobsErrors.dbo.SJI_LTS_ERROR_10022014ALTER TABLE CalJobsErrors.dbo.SJI_LTS_ERROR_10022014                        ADD (LoadDate varchar(8) null,                             processDate datetime null)Msg 911, Level 16, State 4, Server VULCAN, Line 77Database 'ALTER TABLE CalJobsErrors' does not exist. Make sure that the name isentered correctly.C:\CalJobs\SQLCMD\SQLScripts>Vartan |  |  
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts | 
                                        
                                          |  Posted - 2014-10-06 : 13:37:38 
 |  
                                          | Try adding a GO right before the ALTER TABLE. Alternatively you can run 2 sqlcmd commands, referencing different scripts, and then wrap them in a cmd file.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |  
                                          |  |  |  
                                    | Bustaz KoolMaster Smack Fu Yak Hacker
 
 
                                    1834 Posts | 
                                        
                                          |  Posted - 2014-10-06 : 13:48:32 
 |  
                                          | Why do you have parentheses around your column definition? Can you print out your ALTER TABLE ADD Column code and get it to work independently? Try putting parentheses around your variable in the exec statement (exec (@cmd);). Better yet, use "exec sp_ExecuteSQL @cmd" instead of "exec @cmd". No amount of belief makes something a fact. -James Randi
 |  
                                          |  |  |  
                                |  |  |  |  |  |