Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


return value from exec(string)


return value from exec(string)

Author
Message
AndreQ
AndreQ
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 1
I'd like to write a generic procedure that will accept a table name and seed value and return a unique ID. The routine will check the seed value against the table, and if its exists, increment a counter by one.

For example, table is 'Address' and the seed is 'CustKey'. If count of CustKey > 1, try CustKey1. If CustKey1 is there, then CustKey2 and so on. I would like it to return the unique ID.

I've tried to exec('select count(*) from ' + @tableName + ' where IdentityCol = ' + testKey ) and that will return 0 or more, but how would I test what the returned value is?

If this is not the best approach, what approach could I use? For the exec('string'), @@ROWCOUNT was unaffected. Also, it wouldn't recognize any declared variable outside of itself.

Any help is appreciated.
TIA
Andre



jpipes
jpipes
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
Points: 728 Visits: 1
Would you mind posting the table structures. I am not sure whether you have an unnormalized structure or not. If so, it may be advantageous to normalize the structure. Please post the DDL for the table, if possible. Thanks,

Jay



AndreQ
AndreQ
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 1
The example I gave was not the best in that it immediate brought up questions of why I was trying to generate the key in that way in the first place.

The situation is more one of coming into a place where they keys are generated in this way, but for each table and I wanted to make it generic.

The question is more: how do I get out results when EXEC a string and its SQL 6.5 compatibility, otherwise I would have used sp_executesql.

I found a great solution at this website:

http://www.sqlteam.com/item.asp?ItemID=4619

where the author recommends creating a temporary table and putting the results inside as an INSERT.



jpipes
jpipes
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
Points: 728 Visits: 1
I think I could help more if I understood what the overall objective is. Are you trying to get a scalar or table-type value from the procedure? If you are getting a scalar value, use an OUTPUT parameter or the intrinsic RETURNVALUE parameter:


-- Output parameter
DECLARE @OutputParam INT
EXEC proc_GetLastKeyValue @Address, @OutputParam OUTPUT
-- Return Value
DECLARE @r INT
EXEC @r = proc_GetLastKeyValue @Address


Am I anywhere close to what you're asking for?



AndreQ
AndreQ
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 1
No, you're not.

Your last response gives me some good ideas for future projects, but the website that I posted in my last response (the one about temporary tables) gave me a good solution that I'm using.

Thanks anyway.



GRN
GRN
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 1
This one does not need a temp table
USE PUBS
DECLARE @Str   nvarchar(200),
   @Parm nvarchar(20),
   @Cnt   int

SET @Str = 'SELECT @Cnt= COUNT(*) FROM Authors'
SET @Parm = '@Cnt int output'

EXEC sp_executesql @Str , @Parm ,@Cnt OUT

PRINT @Cnt



AndreQ
AndreQ
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 1
The code that I'm dealing with requires a prefix that may or may not be there. The suffix is just a straight counter with leading 0's.

I would have gone with the Max-Count method if that were not the case.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search