Showing posts with label newid. Show all posts
Showing posts with label newid. Show all posts

Wednesday, March 7, 2012

Returning uniqueidentifier after insert

I have a stored procedure that I use to insert data, and I want the sp to
return the newid created. I used to be able to do with select @.@.Identity
with integer fields as the identity field, but it returns nothing when using
uniqueidentifier as the type.
Thanks.Try CASTing it to VARCHAR(36). I.e.,
DECLARE @.myID UNIQUEIDENTIFIER
SET @.myID = NEWID()
SELECT CAST(@.myID AS VARCHAR(36))
"Eagle" <eagletender2001@.yahoo.com> wrote in message
news:eVOq5UFbFHA.3184@.TK2MSFTNGP15.phx.gbl...
>I have a stored procedure that I use to insert data, and I want the sp to
>return the newid created. I used to be able to do with select @.@.Identity
>with integer fields as the identity field, but it returns nothing when
>using uniqueidentifier as the type.
> Thanks.
>
>|||Eagle wrote:
> I have a stored procedure that I use to insert data, and I want the sp to
> return the newid created. I used to be able to do with select @.@.Identity
> with integer fields as the identity field, but it returns nothing when usi
ng
> uniqueidentifier as the type.
When I had the same problem I couldn't find a way to do this.
But the nature of guid allows you to generate it on the client side
and just insert into the table, thus no need to return as you
already have it.|||@.@.identity will return a null value if you are inserting into a table withou
t
any identity columns. As far as I know there is know @.@. server type to
return the last inserted uniqueidentifier. If you are inserting only one
record at a time in the stored procedure, have you considering loading the
UID into a variable, then returning the variable?:
declare @.id uniqueidentifier
set @.id = newid()
insert into table (uid_column, x, y, y) values(@.id, 'x', 'y', 'z')
return @.id
"Eagle" wrote:

> I have a stored procedure that I use to insert data, and I want the sp to
> return the newid created. I used to be able to do with select @.@.Identity
> with integer fields as the identity field, but it returns nothing when usi
ng
> uniqueidentifier as the type.
> Thanks.
>
>|||You can't return an uniqueidentifier with @.@.IDENTITY IF you need to use it
after the insert, you should set a variable using NEWID() before the insert:
CREATE PROCEDURE procName (..., @.id uniqueidentifier OUTPUT) AS
BEGIN
SET @.id = NEWID()
INSERT tableName (..., [id]) VALUES (..., @.id)
END
"Eagle" wrote:

> I have a stored procedure that I use to insert data, and I want the sp to
> return the newid created. I used to be able to do with select @.@.Identity
> with integer fields as the identity field, but it returns nothing when usi
ng
> uniqueidentifier as the type.
> Thanks.
>
>

Saturday, February 25, 2012

Returning random records and NOT similar (random questions)

Hi,

I need to extract randomly 5 records from the table "Questions". Now I use

SELECT TOP 5 FROM Questions ORDERBY NEWID()

And it works. The problem is that I need an additional thing: if SQL
extracts record with ID=4, then it should not extract record with ID=9,
because they are similar. I mean, I'd like something to tell SQL that if it
extracts some questions, then it SHOULD NOT extract other ones.

How can I do it?

Thanks!

Luke"Luke" <nospam@.nospam.com> wrote in message news:<%ejcc.18367$hc5.868453@.news3.tin.it>...
> Hi,
> I need to extract randomly 5 records from the table "Questions". Now I use
> SELECT TOP 5 FROM Questions ORDERBY NEWID()
> And it works. The problem is that I need an additional thing: if SQL
> extracts record with ID=4, then it should not extract record with ID=9,
> because they are similar. I mean, I'd like something to tell SQL that if it
> extracts some questions, then it SHOULD NOT extract other ones.
> How can I do it?
> Thanks!
> Luke

You need to define some logic to say why 4 and 9 are "similar". For
example, should ABS(x-y) > 10 be true for all possible combinations of
numbers in the result set? Or since you're retrieving questions,
perhaps they're in groups, ie. questions 1-20 are on the same topic,
21-40 on a different topic etc., and you want only one random question
from each topic?

Depending on what logic you decide, you might want to consider doing
this in a client application - if the first value you retrieve affects
which ones you can retrieve later, then a cursor-based solution might
be the only way to do it on the server side, and that will be slow. It
may be faster to use a client app which retrieves the maximum and
minimum values (or whatever data you need to reference in your logic),
and then applies your pseudo-random algorithm.

Simon

Tuesday, February 21, 2012

returning limited number of records!

I am using ORDER BY NEWID() to return random record from sql database. how do i go about returning only 5 random records instead of all records.

Thanks.Since SELECT * FROM Table ORDER BY NEWID() will return the rows in a random order, all you need to do is use the TOP keyword to limit the results for that particular query.

SELECT TOP 5 * FROM Table
ORDER BY NEWID()

If, for some reason, that doesn't work, there's the slightly less elegant solution of

SET ROWCOUNT = 5
SELECT * FROM Table
ORDER BY NEWID()
SET ROWCOUNT = 0

I hope this helps.|||it's working perfect.

thanks.