Showing posts with label tablename. Show all posts
Showing posts with label tablename. Show all posts

Tuesday, March 20, 2012

ALTER TABLE In cursor

Hi, All
I have a cursor which sets the value of @.tablename to a user table and
another cursor which set the @.contraint to a valid contraint name.
Why does the statement below produce a incorrect syntax error
alter table @.tablename drop constraint @.constraint
If I substitute the variables with actual values it works fine
Please helpYou can't use parameters in an ALTER TABLE statement. Use:
EXEC ('alter table ' + @.tablename + ' drop constraint ' + @.constraint)
--
Jacco Schalkwijk
SQL Server MVP
"Antony" <Antony@.discussions.microsoft.com> wrote in message
news:89619AFE-048B-4002-AEF4-E7D0A1EA527D@.microsoft.com...
> Hi, All
> I have a cursor which sets the value of @.tablename to a user table and
> another cursor which set the @.contraint to a valid contraint name.
> Why does the statement below produce a incorrect syntax error
> alter table @.tablename drop constraint @.constraint
> If I substitute the variables with actual values it works fine
> Please help

ALTER TABLE In cursor

Hi, All
I have a cursor which sets the value of @.tablename to a user table and
another cursor which set the @.contraint to a valid contraint name.
Why does the statement below produce a incorrect syntax error
alter table @.tablename drop constraint @.constraint
If I substitute the variables with actual values it works fine
Please help
You can't use parameters in an ALTER TABLE statement. Use:
EXEC ('alter table ' + @.tablename + ' drop constraint ' + @.constraint)
Jacco Schalkwijk
SQL Server MVP
"Antony" <Antony@.discussions.microsoft.com> wrote in message
news:89619AFE-048B-4002-AEF4-E7D0A1EA527D@.microsoft.com...
> Hi, All
> I have a cursor which sets the value of @.tablename to a user table and
> another cursor which set the @.contraint to a valid contraint name.
> Why does the statement below produce a incorrect syntax error
> alter table @.tablename drop constraint @.constraint
> If I substitute the variables with actual values it works fine
> Please help

ALTER table command

Hi,

I'm trying to run the ALTER TABLE command using a dynamic string for the
table, like so:

DECLARE @.TableName CHAR
SET @.TableName = 'Customers'
ALTER TABLE @.TableName
ADD ...blah

Is this possible? We know this works:

ALTER TABLE Customers ADD ...blah

It looks like I need a way to convert the CHAR value to a literal or perhaps
even a table ID?

Thanks in advance,
PaulYou would need to use dynamic query...

e.g.
exec('alter table '+@.tb+' add blah')

--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net

"Paul Sampson" <psampson@.uecomm.com.au> wrote in message
news:1061964209.790366@.proxy.uecomm.net.au...
> Hi,
> I'm trying to run the ALTER TABLE command using a dynamic string for the
> table, like so:
> DECLARE @.TableName CHAR
> SET @.TableName = 'Customers'
> ALTER TABLE @.TableName
> ADD ...blah
> Is this possible? We know this works:
> ALTER TABLE Customers ADD ...blah
> It looks like I need a way to convert the CHAR value to a literal or
perhaps
> even a table ID?
> Thanks in advance,
> Paul|||"Paul Sampson" <psampson@.uecomm.com.au> wrote in message news:<1061964209.790366@.proxy.uecomm.net.au>...
> Hi,
> I'm trying to run the ALTER TABLE command using a dynamic string for the
> table, like so:
> DECLARE @.TableName CHAR
> SET @.TableName = 'Customers'
> ALTER TABLE @.TableName
> ADD ...blah
> Is this possible? We know this works:
> ALTER TABLE Customers ADD ...blah
> It looks like I need a way to convert the CHAR value to a literal or perhaps
> even a table ID?
> Thanks in advance,
> Paul

You can use dynamic SQL:

declare @.tablename sysname
set @.tablename = 'Customers'
exec('alter table dbo.' + @.tablename + ' add ...')

See here for more information on dynamic SQL:

http://www.algonet.se/~sommar/dynamic_sql.html

By the way, if you declare a variable as CHAR without a length, it
will default to CHAR(1). For object names, sysname is a better choice.

Simon|||Thanks Simon - a common suggestion and one that I'll be sure to remember.

"Simon Hayes" <sql@.hayes.ch> wrote in message
news:60cd0137.0308270337.2bbb79e4@.posting.google.c om...
> "Paul Sampson" <psampson@.uecomm.com.au> wrote in message
news:<1061964209.790366@.proxy.uecomm.net.au>...
> > Hi,
> > I'm trying to run the ALTER TABLE command using a dynamic string for the
> > table, like so:
> > DECLARE @.TableName CHAR
> > SET @.TableName = 'Customers'
> > ALTER TABLE @.TableName
> > ADD ...blah
> > Is this possible? We know this works:
> > ALTER TABLE Customers ADD ...blah
> > It looks like I need a way to convert the CHAR value to a literal or
perhaps
> > even a table ID?
> > Thanks in advance,
> > Paul
> You can use dynamic SQL:
> declare @.tablename sysname
> set @.tablename = 'Customers'
> exec('alter table dbo.' + @.tablename + ' add ...')
> See here for more information on dynamic SQL:
> http://www.algonet.se/~sommar/dynamic_sql.html
> By the way, if you declare a variable as CHAR without a length, it
> will default to CHAR(1). For object names, sysname is a better choice.
> Simon

Wednesday, March 7, 2012

alter column statement doesn't work, trying to change datatype

I'm trying to do something like this:
ALTER TABLE [tablename] ALTER COLUMN [fieldID] int;
I've tried several versions of this, and the data type is never changed from
numeric. I have removed all indexes on the table and it still doesn't work,
no error messages or anything. I am trying to do this with script because I
have to change like 150 databases and it's part of a much bigger script that
I have to do the same thing on other tables and columns. Any ideas?
thanks,
CoryPerhaps you are altering some other table. Have you checked the owner of the
table? Or, better yet,
owner-qualify the ALTER statement.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Cory Harrison" <charrison@.csiweb.com> wrote in message
news:Ob$zDhUNFHA.3192@.TK2MSFTNGP10.phx.gbl...
> I'm trying to do something like this:
> ALTER TABLE [tablename] ALTER COLUMN [fieldID] int;
> I've tried several versions of this, and the data type is never changed fr
om numeric. I have
> removed all indexes on the table and it still doesn't work, no error messa
ges or anything. I am
> trying to do this with script because I have to change like 150 databases
and it's part of a much
> bigger script that I have to do the same thing on other tables and columns
. Any ideas?
>
> thanks,
> Cory
>
>|||No error messages -- are you sure it's not working? What do you see when
you run sp_help 'tablename' ?
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"Cory Harrison" <charrison@.csiweb.com> wrote in message
news:Ob$zDhUNFHA.3192@.TK2MSFTNGP10.phx.gbl...
> I'm trying to do something like this:
> ALTER TABLE [tablename] ALTER COLUMN [fieldID] int;
> I've tried several versions of this, and the data type is never changed
from
> numeric. I have removed all indexes on the table and it still doesn't
work,
> no error messages or anything. I am trying to do this with script because
I
> have to change like 150 databases and it's part of a much bigger script
that
> I have to do the same thing on other tables and columns. Any ideas?
>
> thanks,
> Cory
>
>|||Wow, you're right, running sp_help does show that it was converted from
numeric to int. I have been looking at it through Enterprise Manager. I
can run the script then look at the design view for the table and it is
unchanged even after closing everything out, refreshing everything, and
trying again. I am becoming more and more agitated with Enterprise Manager
by the day.
thank you,
Cory
"Adam Machanic" <amachanic@.hotmail._removetoemail_.com> wrote in message
news:%23j3gApUNFHA.2544@.TK2MSFTNGP10.phx.gbl...
> No error messages -- are you sure it's not working? What do you see when
> you run sp_help 'tablename' ?
>
> --
> Adam Machanic
> SQL Server MVP
> http://www.datamanipulation.net
> --
>
> "Cory Harrison" <charrison@.csiweb.com> wrote in message
> news:Ob$zDhUNFHA.3192@.TK2MSFTNGP10.phx.gbl...
> from
> work,
> I
> that
>|||> trying again. I am becoming more and more agitated with Enterprise
Manager
> by the day.
And you haven't even read http://www.aspfaq.com/2455 yet...

Saturday, February 25, 2012

Alter column add constraint unique

Is it possible to alter a table column data type AND add a unique
constraint at the same time?
I can get this to work
ALTER TABLE tablename ALTER COLUMN colName DataType(optional size);
and I can get this to work
ALTER TABLE tablename ADD CONSTRAINT UQ_myConstraint UNIQUE
but I can't get both to work at once and don't feel BOL is very
clear.
ThanksNo, you have to do it one at a time.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Jeff User" <jeff31162@.hotmail.com> wrote in message
news:e64ur1p66uqe09e2r5e6njer7r8mc0bms7@.
4ax.com...
> Is it possible to alter a table column data type AND add a unique
> constraint at the same time?
> I can get this to work
> ALTER TABLE tablename ALTER COLUMN colName DataType(optional size);
> and I can get this to work
> ALTER TABLE tablename ADD CONSTRAINT UQ_myConstraint UNIQUE
> but I can't get both to work at once and don't feel BOL is very
> clear.
> Thanks|||Think about the BASICS!!
SQL is a set oriented language. Everything happens at once. If I
created a column, how the hell would I assign a unique value to each
row' Such things would be ordered an there is no order in RM.|||Then any other constraints will also have to be done seperately, for
instance - DEFAULT.
Correct?
Thanks for the replies
Jeff
On Sat, 07 Jan 2006 00:57:06 GMT, Jeff User <jeff31162@.hotmail.com>
wrote:

>Is it possible to alter a table column data type AND add a unique
>constraint at the same time?
>I can get this to work
>ALTER TABLE tablename ALTER COLUMN colName DataType(optional size);
>and I can get this to work
>ALTER TABLE tablename ADD CONSTRAINT UQ_myConstraint UNIQUE
>but I can't get both to work at once and don't feel BOL is very
>clear.
>Thanks|||No, check constraints and default constraints can be defined with the
column:
ALTER TABLE tbl
ADD SomeCol INT NOT NULL DEFAULT (10)
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Jeff User" <jeff31162@.hotmail.com> wrote in message
news:6kdur1lfa8s38pjr730eets11vjpsnggkh@.
4ax.com...
> Then any other constraints will also have to be done seperately, for
> instance - DEFAULT.
> Correct?
> Thanks for the replies
> Jeff
> On Sat, 07 Jan 2006 00:57:06 GMT, Jeff User <jeff31162@.hotmail.com>
> wrote:
>
>|||That is adding a new column. And it works well.
But what about altering an existing column?
Assuming there is no existing Default value:
ALTER TABLE tester
ALTER COLUMN fld9 varchar(30) NOT NULL DEFAULT 'hello'
This doesn't work, I get error near DEFAULT.
I think Adding DEFAULT has to be done seperately. This works:
ALTER TABLE tester
ADD CONSTRAINT makeup_a_name DEFAULT 'test value' FOR fieldName
If there is a way though, to combine these, I would be mighty
interested.
Jeff
On Fri, 6 Jan 2006 22:42:56 -0500, "Adam Machanic"
<amachanic@.hotmail._removetoemail_.com> wrote:

>No, check constraints and default constraints can be defined with the
>column:
>ALTER TABLE tbl
>ADD SomeCol INT NOT NULL DEFAULT (10)
>
>--
>Adam Machanic
>Pro SQL Server 2005, available now
>http://www.apress.com/book/bookDisplay.html?bID=457|||No, there isn't a way to combine them. Column constraints can only be
defined when creating columns...
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Jeff User" <jeff31162@.hotmail.com> wrote in message
news:pmeur1hgvletj6jvu7tsfffol5het9tj7q@.
4ax.com...
> That is adding a new column. And it works well.
> But what about altering an existing column?
> Assuming there is no existing Default value:
> ALTER TABLE tester
> ALTER COLUMN fld9 varchar(30) NOT NULL DEFAULT 'hello'
> This doesn't work, I get error near DEFAULT.
> I think Adding DEFAULT has to be done seperately. This works:
> ALTER TABLE tester
> ADD CONSTRAINT makeup_a_name DEFAULT 'test value' FOR fieldName
> If there is a way though, to combine these, I would be mighty
> interested.
> Jeff
> On Fri, 6 Jan 2006 22:42:56 -0500, "Adam Machanic"
> <amachanic@.hotmail._removetoemail_.com> wrote:
>
>|||IDENTITY property
NEWID()
Have a default based from the result of a UDF value.
Order aside there are times when adding say a column with the IDENTIYY
property is really useful - consider data cleansing, siutation where you are
merging the output from two systems to get rid of duplicates.
Why go to the hassle of adding a new column and then having to write your
own unique number generator, simple type the extra 20 or so characters and
the ALTER TABLE statement will do it for you - KISS (Keep It Simple Sweet)
rather than spinning out the work required so you get paid more.
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1136602552.703133.30640@.f14g2000cwb.googlegroups.com...
> Think about the BASICS!!
> SQL is a set oriented language. Everything happens at once. If I
> created a column, how the hell would I assign a unique value to each
> row' Such things would be ordered an there is no order in RM.
>|||You would have to drop the existing DEFAULT constraint first. So
BEGIN TRANSACTION
ALTER TABLE .. DROP CONSTRAINT old_default
ALTER TABLE .. ADD COSNTRAINT new_default DEFAULT .. FOR column
COMMIT TRANSACTION
With the proper transaction isolation level, the transaction wrapper
prevent changes between the two statements.
Gert-Jan
Jeff User wrote:
> That is adding a new column. And it works well.
> But what about altering an existing column?
> Assuming there is no existing Default value:
> ALTER TABLE tester
> ALTER COLUMN fld9 varchar(30) NOT NULL DEFAULT 'hello'
> This doesn't work, I get error near DEFAULT.
> I think Adding DEFAULT has to be done seperately. This works:
> ALTER TABLE tester
> ADD CONSTRAINT makeup_a_name DEFAULT 'test value' FOR fieldName
> If there is a way though, to combine these, I would be mighty
> interested.
> Jeff
> On Fri, 6 Jan 2006 22:42:56 -0500, "Adam Machanic"
> <amachanic@.hotmail._removetoemail_.com> wrote:
>

Alter Column

Hi
How come this doesnt work?
ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SET'.
A default in SQL Server is handled as a constraint. So you would need something like:
ALTER TABLE tblname
ADD CONSTRAINT cnstname DEFAULT CURRENT_TIMESTAMP FOR columnname
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bob Trask" <bob@.techset.net> wrote in message news:uLy7qEctEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>
|||Bob,
You need to add a constraint to the column:
ALTER TABLE [tablename]
ADD CONSTRAINT <constraintname>
DEFAULT getdate() FOR [columnB]
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Bob Trask wrote:
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>
|||Bob
CREATE TABLE Test
(
col1 INT NOT NULL PRIMARY KEY,
col2 DATETIME NOT NULL
)
INSERT INTO Test VALUES (1,'20040101')
GO
ALTER TABLE Test ADD CONSTRAINT
myconst_col2 DEFAULT GETDATE() FOR col2
GO
INSERT INTO Test VALUES (2,DEFAULT)
GO
SELECT * FROM Test
GO
DROP TABLE Test
"Bob Trask" <bob@.techset.net> wrote in message
news:uLy7qEctEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>
|||Cheers People, great help thanks!
"Bob Trask" <bob@.techset.net> wrote in message
news:uLy7qEctEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>

Alter Column

Hi
How come this doesnt work?
ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SET'.A default in SQL Server is handled as a constraint. So you would need something like:
ALTER TABLE tblname
ADD CONSTRAINT cnstname DEFAULT CURRENT_TIMESTAMP FOR columnname
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bob Trask" <bob@.techset.net> wrote in message news:uLy7qEctEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>|||Bob,
You need to add a constraint to the column:
ALTER TABLE [tablename]
ADD CONSTRAINT <constraintname>
DEFAULT getdate() FOR [columnB]
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Bob Trask wrote:
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>|||Bob
CREATE TABLE Test
(
col1 INT NOT NULL PRIMARY KEY,
col2 DATETIME NOT NULL
)
INSERT INTO Test VALUES (1,'20040101')
GO
ALTER TABLE Test ADD CONSTRAINT
myconst_col2 DEFAULT GETDATE() FOR col2
GO
INSERT INTO Test VALUES (2,DEFAULT)
GO
SELECT * FROM Test
GO
DROP TABLE Test
"Bob Trask" <bob@.techset.net> wrote in message
news:uLy7qEctEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>|||Cheers People, great help thanks!
"Bob Trask" <bob@.techset.net> wrote in message
news:uLy7qEctEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Hi
> How come this doesnt work?
>
> ALTER TABLE [tablename] ALTER COLUMN [columnB] SET DEFAULT getdate()
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'SET'.
>

Sunday, February 12, 2012

all tablenames having a specific column

Hi
Is there a way to get a list of all tables having a specific tablename
Ex. Give me all tables names having a column name 'customer_id'
Kind Regards
RoelHi,
select object_name(id) , name from syscolumns where name = 'customer_id'
Thanks
Hari
MCDBA
"Roel" <rvdbrand@.tycoint.com> wrote in message
news:#nWOtpa6DHA.2472@.TK2MSFTNGP10.phx.gbl...
> Hi
> Is there a way to get a list of all tables having a specific tablename
> Ex. Give me all tables names having a column name 'customer_id'
> Kind Regards
> Roel
>|||SELECT c.table_name
FROM information_schema.columns c
INNER JOIN information_schema.tables t
ON c.table_name = t.table_name
WHERE t.table_type = 'BASE TABLE'
AND c.column_name = 'customer_id'
Jacco Schalkwijk
SQL Server MVP
"Roel" <rvdbrand@.tycoint.com> wrote in message
news:%23nWOtpa6DHA.2472@.TK2MSFTNGP10.phx.gbl...
> Hi
> Is there a way to get a list of all tables having a specific tablename
> Ex. Give me all tables names having a column name 'customer_id'
> Kind Regards
> Roel
>|||Syscolumns not only includes the columns in each table, but also columns for
views and functions and parameters of stored procedures and functions.
What you want is:
select object_name(id) AS table_name , name from syscolumns where name ='customer_id'
AND OBJECTPROPERTY(id, 'IsUserTable') = 1
but using the information_schema views (see my other post) is simpler.
Jacco Schalkwijk
SQL Server MVP
"Hari Prasad" <hari_prasad_k@.hotmail.com> wrote in message
news:uPj021a6DHA.1664@.TK2MSFTNGP11.phx.gbl...
> Hi,
> select object_name(id) , name from syscolumns where name = 'customer_id'
> Thanks
> Hari
> MCDBA
> "Roel" <rvdbrand@.tycoint.com> wrote in message
> news:#nWOtpa6DHA.2472@.TK2MSFTNGP10.phx.gbl...
> > Hi
> >
> > Is there a way to get a list of all tables having a specific tablename
> > Ex. Give me all tables names having a column name 'customer_id'
> >
> > Kind Regards
> > Roel
> >
> >
>|||SELECT SO.Name
FROM SysObjects SO INNER JOIN SysColumns SC
ON SO.ID = SC.ID
WHERE (
SO.XType = 'U'
AND SC.Name = 'YourColumnName'
)
Cheers,
James Goodman MCSE, MCDBA
http://www.angelfire.com/sports/f1pictures|||thx
all queyies have the same output
Regards
"Roel" <rvdbrand@.tycoint.com> wrote in message
news:#nWOtpa6DHA.2472@.TK2MSFTNGP10.phx.gbl...
> Hi
> Is there a way to get a list of all tables having a specific tablename
> Ex. Give me all tables names having a column name 'customer_id'
> Kind Regards
> Roel
>