Showing posts with label solve. Show all posts
Showing posts with label solve. Show all posts

Monday, March 26, 2012

How to remove a Primary Key you don't know?

Hello Guys,

I need some help with a question I've been struggling to solve all day. I'm new to Transact-SQL and thus tried to solve my little problem by reading the language reference, but I didn't find what I was looking for.

Here's the scenario: I want to remove a table's primary key and replace it with a new one. Removing the pk is usually done with:

1) ALTER TABLE tablename DROP CONSTRAINT constraint_name; GO

Unfortunatley, I don't know constraint_name and the simple MySQL command

ALTER TABLE tablename DROP PRIMARY KEY

doesn't seem to work. To get constraint_name I made a query:

2) SELECT name from sys.key_constraints where parent_object_id = (select object_id from sys.tables where name = 'tablename')

Now, here's the core of my problem: I need to link statements 1) and 2) and tried

ALTER TABLE tablename DROP CONSTRAINT (SELECT name from sys.key_constraints where parent_object_id = (select object_id from sys.tables where name = 'tablename'))

but that doesn't, I get a syntax error.

Do you have a clue? I'm sure its a simple problem for an experienced programmer, but I don't get it. Or do you know another way of removing the primary key without having to state constraint_name explicitely?

Any help is appreciated!

Thanks a bunch,

adunak

You need to use dynamic SQL to execute the ALTER TABLE statement if you want to parameterize the constraint name. Most of the DDL statement parameters in TSQL cannot be parameterized by using variables or expressions. So do below instead:

declare @.const nvarchar(200)

set @.const = QUOTENAME((

select kc.name

from sys.key_constraints as kc

where kc.parent_object_id = object_id('tablename') and kc.type = 'PK'

))

exec('ALTER TABLE tablename DROP CONSTRAINT ' + @.const)

|||

Hi Umachandar,

your hint worked well - thanks a lot for helping me out :-)

Adunak

Friday, March 23, 2012

How to reference the primary key of a newly added record in trigger?

Please help me somebody solve my problem with my first :o trigger:

ALTER TRIGGER partner_update
ON dbo.partner
FOR UPDATE
AS
INSERT INTO partner (name) SELECT name FROM deleted
UPDATE invoice SET id_partner= *** WHERE id_partner = (SELECT id_partner FROM deleted)


*** - here I want to add a "reference" to the newly added record's automatically generated primary key (not to the updated!) Is it possible?I'd use:ALTER TRIGGER partner_update
ON dbo.partner
FOR UPDATE
AS
BEGIN
INSERT INTO partner (name)
SELECT name FROM deleted

UPDATE o
SET id_partner= i.id_partner
FROM dbo.invoice AS o
INNER JOIN deleted AS d
ON (d.id_partner = o.id_partner)
LEFT OUTER JOIN inserted AS i
ON (i.id_partner = o.id_partner)
ENDThis trigger takes a rather perverse view of the universe, allowing for the possiblity that some low-down, high-smelling, bundle-of-dung might possibly change the value of the id_partner column. The invoice table might or might not like that, since the trigger would then NULL out the invoice.id_partner column!

-PatP

Sunday, February 19, 2012

How To Query A Linked Server

Hey all. I'm having a slight problem and was hoping I could use
linked servers to solve it. Basically I've developed a web app
locally using SQL 2005 Express and need to import the data to a web
hosting facility SQL 2005 server. I can see the production server in
management studio and have set up a linked server. However I'm not
sure how to query the database and then ultimately select from my
local db and insert it into the new db tables.
First problem is the linked server address. It's the following:
sqlXXXX.mssqlservers.com
When I query the linked server I get this.
The object name 'sqlXXXX.mssqlservers.com.MyDB.dbo.MyTable' contains
more than the maximum number of prefixes. The maximum is 3.
Is the linked server route the best way for me to import this data? I
need to get the data in ASAP for this client.
Thanks.Hi,
Use: '[sqlXXXX.mssqlservers.com].MyDB.dbo.MyTable'
--
Thank you,
Daniel Jameson
SQL Server DBA
Children's Oncology Group
www.childrensoncologygroup.org
<rh1200la@.gmail.com> wrote in message
news:1190655841.782199.125430@.19g2000hsx.googlegroups.com...
> Hey all. I'm having a slight problem and was hoping I could use
> linked servers to solve it. Basically I've developed a web app
> locally using SQL 2005 Express and need to import the data to a web
> hosting facility SQL 2005 server. I can see the production server in
> management studio and have set up a linked server. However I'm not
> sure how to query the database and then ultimately select from my
> local db and insert it into the new db tables.
> First problem is the linked server address. It's the following:
> sqlXXXX.mssqlservers.com
> When I query the linked server I get this.
> The object name 'sqlXXXX.mssqlservers.com.MyDB.dbo.MyTable' contains
> more than the maximum number of prefixes. The maximum is 3.
> Is the linked server route the best way for me to import this data? I
> need to get the data in ASAP for this client.
> Thanks.
>