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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 dynamic sql

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-22 : 06:59:38
andrew writes "Andrew Krenitz writes "I have the created the following:

CREATE TABLE #crosstable (
State char(2),
LOCNCODE char(11),
CUSTCLAS char(15),
CPRCSTNM char(15),
CUSTNMBR char(15),
totals numeric(19,5))

I want to add a column to this table:

declare @appendString Nvarchar(4000)
set @appendString = 'ALTER TABLE #crosstable ADD test varchar(10)'
EXECUTE sp_executesql @appendString

This will not add the column.

Please let me know what corections I need to make.

Thanks


Andy""

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-22 : 07:08:08
Altering the table dynamically is not recommended
Why do you want to do this?
Refer this more on Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mpetanovitch
Yak Posting Veteran

52 Posts

Posted - 2005-08-22 : 14:00:17
agreed, dynamic sql in my opinion, is bad practice in almost every aspect. However I'm sure some disagree. I tend to stay away from it.

Now, from your query, the reason why its not working is because your local table variable is not in scope to you dynamic sql execution. Try creating a global table ##.


Mike Petanovitch
Go to Top of Page
   

- Advertisement -