Showing posts with label return. Show all posts
Showing posts with label return. Show all posts

Wednesday, March 21, 2012

Qusetion about return values from EXEC('select count(*) from xTable')

Hello everybody!

As the topic:

Can i get the value "count(*)" from EXEC('select count(*) from xTable')

Any helps will be usefull! Thanks!

Hi,

Yes you can do that. it will return a table with one column and a row. Please make sure to specify column name for count(*)

'select count(*) as RowCount from xTable'

|||

How about this ;

create table #results (
cnt int
)

insert #results
exec ('select count(*) from master..sysdatabases')

select cnt from #results

|||

Hi

You can return a parameter from executing a string , but you need to use sp_executesql , like

DECLARE @.iCount INT

EXEC sp_executesql N'SELECT @.iCount= COUNT(*) FROM xTable', 'N'@.iCount INT OUT', @.iCount OUT

SELECT @.iCount

|||

Thanks Shallu, Rod Colledge, and NB2006.

I will try to test whether it works. Thanks !!

Not good at english! Sorry!!

Tuesday, March 20, 2012

Quick TSQL?

Hey all... I have a few tables that I am joining and need to know how to set a value from the return to a different column:

example:

SELECT

Prospect.ProspectName AS P1, AccountShipTo.ShipToName AS [Account Name], ProposalHeader.PropHCity AS City, ProposalHeader.PropHState AS State,

ProposalHeader.PropHNumb AS [Proposal ID], ProposalHeader.PropHRevNumb AS Rev, ProposalHeader.PropHCreateDate AS [Creation Date],

ProposalHeader.update_timestamp AS [Last Edit Date]

FROM ProposalHeader LEFT OUTER JOIN

AccountShipTo ON ProposalHeader.PropHShipTo = LTRIM(AccountShipTo.ShipToCust) LEFT OUTER JOIN

Prospect ON ProposalHeader.PropHBillTo = LTRIM(Prospect.ProspectNumb)

WHERE (ProposalHeader.Alias = N'billb')

GROUP BY ProposalHeader.PropHRevNumb, AccountShipTo.ShipToName, ProposalHeader.PropHCity, ProposalHeader.PropHState,

ProposalHeader.PropHNumb, ProposalHeader.PropHCreateDate, ProposalHeader.update_timestamp, Prospect.ProspectName

ORDER BY [Proposal ID]

I need P1 value (Test - Timberline Corp)to be in the Account Name column (NULL)... any ideas?

P1 Account Name City State Proposal ID Rev Creation Date Last Edit Date

NULL Samples, Inc. High Point NC Samples1 1 2007-07-25 2007-07-30

Test - Timberline Corp NULL Rapid City SD test1 1 2007-07-31 2007-07-31

(2 row(s) affected)

Any help would be appreciated... thanks!

Have you tried coalesce(AccountShipTo.ShipToName, Prospect.ProspectName) or isnull(AccountShipTo.ShipToName, Prospect.ProspectName)
|||

Very coo! Thanks... Smile

Did this....

SELECT COALESCE (AccountShipTo.ShipToName, Prospect.ProspectName) AS [Account Name]

quick transaction inside a stored procedure question

a transaction block inside a sproc

if after one of the tran statements, I bust out of the sproc with a return statement

do I have to rollback explicitly first or does the tran rollback automatically?

appreciate the tip

After returning to another procedure the @.@.TRANCOUNT should be still increased by 1. You can test that by printing out the @.@.trancount variables in both procedure. In common, I would always handle the transcation context on my own by settings explizit transaction heaviour.

HTH, jens Suessmeyer.|||thanks jens|||

Could you please rate the thread as solved or helpful or whatever, that its no longer present as unanswerd ?

Thanks, Jens.

Friday, March 9, 2012

Quick question: how do I get the name of the current database?

Is there an sp_zzzzzz function to return the name of the current database?
I would like to use this name as a variable in a stored procedure in order
to create names for further databases (by appending a tag, such as
MYDATABASE_BLOB001, ..._BLOB002 etc.

Thanks.SELECT DB_NAME()

Why create a database from a Stored Procedure?
--
David Portas
SQL Server MVP
--|||"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:ct5isn$9m3$1$8302bc10@.news.demon.co.uk...
> Is there an sp_zzzzzz function to return the name of the current database?
> I would like to use this name as a variable in a stored procedure in order
> to create names for further databases (by appending a tag, such as
> MYDATABASE_BLOB001, ..._BLOB002 etc.
>
> Thanks.

select db_name()

It might be worth considering why you need multiple databases - could
BLOB001 be part of a key in a table instead? This discussion applies to
tables, not databases, but the principle is exactly the same:

http://www.sommarskog.se/dynamic_sql.html#Sales_yymm

Simon|||The issue is that we have one database with our adjacency list in and then
one or more with our media blobs in. Our users won't be using full SQL
server, so we have 2Gb limit on MSDE. When one of the blob databases
reaches close to 2Gb, we rollover to the next blob database. In my
adjacency table I store the blob database ID and Key for the blob associated
with each node. I also have a table with all of the blob databases listed,
so I can lookup and find which database the ID refers to.

Can't help it. Customers are cheapskates ;)

"Simon Hayes" <sql@.hayes.ch> wrote in message
news:41f65413$1_3@.news.bluewin.ch...
> "Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
> message news:ct5isn$9m3$1$8302bc10@.news.demon.co.uk...
>> Is there an sp_zzzzzz function to return the name of the current
>> database? I would like to use this name as a variable in a stored
>> procedure in order to create names for further databases (by appending a
>> tag, such as MYDATABASE_BLOB001, ..._BLOB002 etc.
>>
>>
>> Thanks.
>>
>>
>>
> select db_name()
> It might be worth considering why you need multiple databases - could
> BLOB001 be part of a key in a table instead? This discussion applies to
> tables, not databases, but the principle is exactly the same:
> http://www.sommarskog.se/dynamic_sql.html#Sales_yymm
> Simon|||"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:ct5l4s$ig$1$8300dec7@.news.demon.co.uk...
> The issue is that we have one database with our adjacency list in and then
> one or more with our media blobs in. Our users won't be using full SQL
> server, so we have 2Gb limit on MSDE. When one of the blob databases
> reaches close to 2Gb, we rollover to the next blob database. In my
> adjacency table I store the blob database ID and Key for the blob
associated
> with each node. I also have a table with all of the blob databases
listed,
> so I can lookup and find which database the ID refers to.
> Can't help it. Customers are cheapskates ;)

Something to be aware of: I think that MSDE may also limit you on the number
of databases you are allowed so there may be a limit on the number of times
you can "rollover" to another database.

Brian.

www.cryer.co.uk/brian

Wednesday, March 7, 2012

Quick DISTINCT question

Hello all!
I know the following will work,
"SELECT DISTINCT Name, MIN(Sign) AS Sign
FROM Profile
GROUP BY Name"
Will return 2 columns, Name and Sign.
But what if I want more than just the two columns, and I need four to be
listed, but using the same code above. Just not sure how to add additional
columns without getting errors. Is this even possible?
TIA!!!
RudyIf you can show us some sample data and the required output we can come up
with some queries. Without that, you either add those additional columns to
the GROUP BY clause, or have then in the SELECT, within an aggregate
function.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Rudy" <Rudy@.discussions.microsoft.com> wrote in message
news:5EC4A04B-AD95-40C6-BBEE-D8A1E9B3BF52@.microsoft.com...
Hello all!
I know the following will work,
"SELECT DISTINCT Name, MIN(Sign) AS Sign
FROM Profile
GROUP BY Name"
Will return 2 columns, Name and Sign.
But what if I want more than just the two columns, and I need four to be
listed, but using the same code above. Just not sure how to add additional
columns without getting errors. Is this even possible?
TIA!!!
Rudy|||Yes, you need to decide, for each of those other columns, which of the
possible multiple values that exists should be output by the query...
Since you are Grouping By Name, that means you will get one row in your
output per disntinct value of Name. There may be many rows in the original
Table for each value of Name, each with different values for these other
columns... So for each, you must tell query whether to output the Min(), the
Max(), the Sum(), AVG(), or whatever...
Select Name, MIN(Sign) AS Sign,
Min(Col1), Max(Col2), etc...
From Profile
Group By Name
If you want ALL the values of these other columns listed, as:
Name Col1 Col2
John 1 AA
John 2 AB
John 3 AC
etc.
then you can't group just by name, you need to add the other columns to the
group By clause
"Rudy" wrote:

> Hello all!
> I know the following will work,
> "SELECT DISTINCT Name, MIN(Sign) AS Sign
> FROM Profile
> GROUP BY Name"
> Will return 2 columns, Name and Sign.
> But what if I want more than just the two columns, and I need four to be
> listed, but using the same code above. Just not sure how to add additional
> columns without getting errors. Is this even possible?
> TIA!!!
> Rudy|||also, look at the with rollup and with group options for group, then you can
do stuff like:
Select Name, col1, min(Sign)
From Profile
Group By Name, col1 with rollup
and you will get all sorts of different levels...
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
news:406DA4BA-6217-431D-B838-CA56B96C3453@.microsoft.com...
> Yes, you need to decide, for each of those other columns, which of the
> possible multiple values that exists should be output by the query...
> Since you are Grouping By Name, that means you will get one row in your
> output per disntinct value of Name. There may be many rows in the original
> Table for each value of Name, each with different values for these other
> columns... So for each, you must tell query whether to output the Min(),
> the
> Max(), the Sum(), AVG(), or whatever...
> Select Name, MIN(Sign) AS Sign,
> Min(Col1), Max(Col2), etc...
> From Profile
> Group By Name
> If you want ALL the values of these other columns listed, as:
> Name Col1 Col2
> John 1 AA
> John 2 AB
> John 3 AC
> etc.
> then you can't group just by name, you need to add the other columns to
> the
> group By clause
>
> "Rudy" wrote:
>|||Thanks you everyone for your suggestions! CBretana, your answer did the
trick. Thank!!!
Rudy
"CBretana" wrote:
> Yes, you need to decide, for each of those other columns, which of the
> possible multiple values that exists should be output by the query...
> Since you are Grouping By Name, that means you will get one row in your
> output per disntinct value of Name. There may be many rows in the original
> Table for each value of Name, each with different values for these other
> columns... So for each, you must tell query whether to output the Min(), t
he
> Max(), the Sum(), AVG(), or whatever...
> Select Name, MIN(Sign) AS Sign,
> Min(Col1), Max(Col2), etc...
> From Profile
> Group By Name
> If you want ALL the values of these other columns listed, as:
> Name Col1 Col2
> John 1 AA
> John 2 AB
> John 3 AC
> etc.
> then you can't group just by name, you need to add the other columns to th
e
> group By clause
>
> "Rudy" wrote:
>