Showing posts with label key. Show all posts
Showing posts with label key. Show all posts

Wednesday, March 21, 2012

Name of primary key

What 's the way to know
the name of the column that is
the primary key of a tableTry this one:

select 'Table ['+su.name+'.'+so.name+']',sc.name
from sysindexes sy
join sysobjects so on so.id=sy.id and so.xtype='U'
join sysusers su on su.uid=so.uid
join sysindexkeys si on si.id=so.id and si.indid=sy.indid
join syscolumns sc on sc.id=so.id and sc.colid=si.colid
join sysobjects sop on sop.parent_obj=so.id and sop.xtype='PK' and sop.name=sy.name
where sy.indid not in(0,255)|||Thank you

forgot that existed sysindexes|||Thank you very much

Monday, February 20, 2012

My own little piece of SQL Wierdness

This has me stumped.
I have a parent and a child table with a constraint on the parent's foreign
key in
the child.
So with this in mind, every once in a while when I am exporting data, I get
an error
that there is a row in the child table with and ID that is not in the parent
table.
One of our programmers is using ADO transactions and beginning it in source
code (not
a proc). Then he adds a row to the parent table, gets the ID in one proc an
d adds a
row to the child table in another proc. Then he commits or aborts when he g
ets back
to code.
Can you think of any reason why the row in the child table is there, but the
row in
the parent isn't there when exporting data?
Thanks.Do you have foreign key constraints on your tables? If so, then it sounds
like a query issue. Otherwise, my guess is the programs arent doing
"something" right?
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Mike Malter" <mikemalter@.newsgroup.nospam> wrote in message
news:ut9Jr40pFHA.3524@.tk2msftngp13.phx.gbl...
> This has me stumped.
> I have a parent and a child table with a constraint on the parent's
> foreign key in the child.
> So with this in mind, every once in a while when I am exporting data, I
> get an error that there is a row in the child table with and ID that is
> not in the parent table.
> One of our programmers is using ADO transactions and beginning it in
> source code (not a proc). Then he adds a row to the parent table, gets
> the ID in one proc and adds a row to the child table in another proc.
> Then he commits or aborts when he gets back to code.
> Can you think of any reason why the row in the child table is there, but
> the row in the parent isn't there when exporting data?
> Thanks.
>|||The foreign key constraint is not being enforced.
exec sp_helpconstraint N'tableName'
The second result set contains a list of constraints. Check the
status_enabled column for the foreign key constraint in question. If it's
Disabled, then the constraint will not be enforced.
"Mike Malter" <mikemalter@.newsgroup.nospam> wrote in message
news:ut9Jr40pFHA.3524@.tk2msftngp13.phx.gbl...
> This has me stumped.
> I have a parent and a child table with a constraint on the parent's
foreign key in
> the child.
> So with this in mind, every once in a while when I am exporting data, I
get an error
> that there is a row in the child table with and ID that is not in the
parent table.
> One of our programmers is using ADO transactions and beginning it in
source code (not
> a proc). Then he adds a row to the parent table, gets the ID in one proc
and adds a
> row to the child table in another proc. Then he commits or aborts when he
gets back
> to code.
> Can you think of any reason why the row in the child table is there, but
the row in
> the parent isn't there when exporting data?
> Thanks.
>

my new function not found

I issue this command in QA:
CREATE FUNCTION encrypt_pair (@.data VARCHAR(14) , @.key char(30) )
RETURNS VARCHAR (60) AS
BEGIN
DECLARE @.encryptedvars VARCHAR(60)
EXEC master..xp_aes_encrypt @.data,@.key,@.encryptedvars OUTPUT
RETURN @.encryptedvars
END

If I run it again, I get an error that it already exists (duh).
Next I run:

select student_id, student_ssn,
encrypt_pair(student_ssn,'000000000000000000000000 00000000') from
student_ssn

and I get the error:
Server: Msg 195, Level 15, State 10, Line 1
'encrypt_pair' is not a recognized function name.
I don't get whats wrong!
Thanks
RobObject Owner...Object Owner

CREATE FUNCTION dbo.encrypt_pair (@.data VARCHAR(14) , @.key char(30) )
RETURNS VARCHAR (60) AS
BEGIN
DECLARE @.encryptedvars VARCHAR(60)
EXEC master..xp_aes_encrypt @.data,@.key,@.encryptedvars OUTPUT
RETURN @.encryptedvars
END

select student_id, student_ssn,
dbo.encrypt_pair(student_ssn,'00000000000000000000 000000000000') from
student_ssn

--
----------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@.epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

"rcamarda" <rcamarda@.cablespeed.com> wrote in message
news:1128344611.711934.195110@.g43g2000cwa.googlegr oups.com...
>I issue this command in QA:
> CREATE FUNCTION encrypt_pair (@.data VARCHAR(14) , @.key char(30) )
> RETURNS VARCHAR (60) AS
> BEGIN
> DECLARE @.encryptedvars VARCHAR(60)
> EXEC master..xp_aes_encrypt @.data,@.key,@.encryptedvars OUTPUT
> RETURN @.encryptedvars
> END
> If I run it again, I get an error that it already exists (duh).
> Next I run:
> select student_id, student_ssn,
> encrypt_pair(student_ssn,'000000000000000000000000 00000000') from
> student_ssn
> and I get the error:
> Server: Msg 195, Level 15, State 10, Line 1
> 'encrypt_pair' is not a recognized function name.
> I don't get whats wrong!
> Thanks
> Rob|||Thanks Mike, that thought never crossed my mind!