Hi,
I'm trying to write a function check if a column exists on a table, and
creates it if it doesn't. The line that the query analyzer is citing
is noted. It seems unhappy taking variables in the ALTER TABLE
command. I can think of a hack around this, but I'm hoping there's a
better way? Muchas gracias in advance :)
-DJ
Code follows
CREATE FUNCTION fieldCreator
(@TableName varchar(20),
@FieldName varchar(20))
RETURNS BIT
AS
BEGIN
if (EXISTS (SELECT count (objname)
FROM ::fn_listextend edproperty (NULL, 'user', 'dbo', 'table',
@TableName, 'column', @FieldName)
GROUP BY objname))
BEGIN
ALTER TABLE @TableName ADD @FieldName int DEFAULT 4 --ERRORS HERE!!!
EXEC sp_addextendedp roperty 'caption', 'Created by script for
analysis', 'user', dbo, 'table', @TableName, 'column', @FieldName
return 1
END
return 0
END
GO
I'm trying to write a function check if a column exists on a table, and
creates it if it doesn't. The line that the query analyzer is citing
is noted. It seems unhappy taking variables in the ALTER TABLE
command. I can think of a hack around this, but I'm hoping there's a
better way? Muchas gracias in advance :)
-DJ
Code follows
CREATE FUNCTION fieldCreator
(@TableName varchar(20),
@FieldName varchar(20))
RETURNS BIT
AS
BEGIN
if (EXISTS (SELECT count (objname)
FROM ::fn_listextend edproperty (NULL, 'user', 'dbo', 'table',
@TableName, 'column', @FieldName)
GROUP BY objname))
BEGIN
ALTER TABLE @TableName ADD @FieldName int DEFAULT 4 --ERRORS HERE!!!
EXEC sp_addextendedp roperty 'caption', 'Created by script for
analysis', 'user', dbo, 'table', @TableName, 'column', @FieldName
return 1
END
return 0
END
GO
Comment