Showing posts with label returns. Show all posts
Showing posts with label returns. Show all posts

Monday, March 26, 2012

Fetching Data from Table result

This may not be possible but if I don't ask...
I have a SQL query which returns something like :
Category Value1 Value2 Value3 Value 4
A 10 12 14 15
B 8 22 44 55
C 5 33 55 88
I have a table which displays the above data like:
Category Values
C 5
33
55
88
B 8
22
44
55
A 10
12
14
15
Note the ordering of the resultant table. In the Table Properties | Sorting
I have a function which returns the highest value given a set of values.
Hence the sorting is set to Code.HighestValue (Value1, Value2, Value3,
Value4). Sure there's a better way to do this and am open to suggestions.
Finally we come to my problem. I want to display 88 (highest value from
category C) in another part of the report. Is there a way to do this other
than changing the sorting in the SQL query so I can grab the first row?
MartinYeah as I see you have written code to find the highest value.
Just write another function with the same parameters as
HighestValue that returns the highest value.
In your code just compare value 1 ,value 2 ,value 3 ,value 4.
Then use the return value of the function whereever you want.
Hope this helps
>--Original Message--
>This may not be possible but if I don't ask...
>I have a SQL query which returns something like :
>Category Value1 Value2 Value3 Value 4
>A 10 12 14 15
>B 8 22 44 55
>C 5 33 55 88
>I have a table which displays the above data like:
>Category Values
>C 5
> 33
> 55
> 88
>B 8
> 22
> 44
> 55
>A 10
> 12
> 14
> 15
>Note the ordering of the resultant table. In the Table
Properties | Sorting
>I have a function which returns the highest value given a
set of values.
>Hence the sorting is set to Code.HighestValue (Value1,
Value2, Value3,
>Value4). Sure there's a better way to do this and am open
to suggestions.
>Finally we come to my problem. I want to display 88
(highest value from
>category C) in another part of the report. Is there a way
to do this other
>than changing the sorting in the SQL query so I can grab
the first row?
>
>Martin
>.
>

Fetch Returning Duplicate Last Record

Ok, this thing is returning the last record twice. If I have only one record it returns it twice, multiple records gives me the last one twice. I am sure some dumb pilot error is involved, HELP!

Thanks in advance, Larry

ALTER FUNCTION dbo.TestFoodDisLikes

(

@.ResidentID int

)

RETURNS varchar(250)

AS

BEGIN

DECLARE @.RDLike varchar(50)

DECLARE @.RDLikeList varchar(250)

BEGIN

SELECT @.RDLikeList = ''

DECLARE RDLike_cursor CURSOR

LOCAL SCROLL STATIC

FOR

SELECT FoodItem

FROM tblFoodDislikes

WHERE (ResidentID = @.ResidentID) AND (Breakfast = 'True')

OPEN RDLike_cursor

FETCH NEXT FROM RDLike_cursor

INTO @.RDLike

SELECT @.RDLikeList = @.RDLike

WHILE @.@.FETCH_STATUS = 0

BEGIN

FETCH NEXT FROM RDLike_cursor

INTO @.RDLike

SELECT @.RDLikeList = @.RDLikeList + ', ' + @.RDLike

END

CLOSE RDLike_cursor

DEALLOCATE RDLike_cursor

END

RETURN @.RDLikeList

END

Your selecting into the cursor before any operation in the begin statement. ****

ALTER FUNCTION dbo.TestFoodDisLikes
(
@.ResidentID int
)
RETURNS varchar(250)
AS
BEGIN
DECLARE @.RDLike varchar(50)
DECLARE @.RDLikeList varchar(250)
BEGIN
SELECT @.RDLikeList = ''
DECLARE RDLike_cursor CURSOR
LOCAL SCROLL STATIC
FOR
SELECT FoodItem
FROM tblFoodDislikes
WHERE (ResidentID = @.ResidentID) AND (Breakfast = 'True')
OPEN RDLike_cursor
FETCH NEXT FROM RDLike_cursor
INTO @.RDLike
SELECT @.RDLikeList = @.RDLike
WHILE @.@.FETCH_STATUS = 0
BEGIN --****

SELECT @.RDLikeList = @.RDLikeList + ', ' + @.RDLike--****
FETCH NEXT FROM RDLike_cursor INTO @.RDLike--****

END--****
CLOSE RDLike_cursor
DEALLOCATE RDLike_cursor
END
RETURN @.RDLikeList
END

|||

Steve,

It still duplicates one record, that just moved it to the beginning. If I am expecting to see somethimg like 'Ham, Beans, Biscuit, Apples, Beets' I get 'Ham, Ham, Beans, Biscuit, Apples, Beets'. Looks like the second part of the FETCH is starting all over again

|||Actually I would avoid using a cursor for this, according to the Northwind database it would be something like this:

DECLARE @.Cities VARCHAR(8000)

SET @.Cities = ''

SELECT @.Cities = CASE @.Cities

WHEN '' THEN City

ELSE @.Cities + ', ' + City

END

from CUstomers

Group by City

Select @.Cities

According to your problem it would be

DECLARE @.ResidentID VARCHAR(8000)

DECLARE @.FoodItems VARCHAR(8000)

SET @.FoodItems = ''

SELECT @.FoodItems =CASE @.FoodItems

WHEN '' THEN FoodItem

ELSE @.FoodItems + ', ' + FoodItem

END

from tblFoodDislikes

WHERE (ResidentID = @.ResidentID) AND (Breakfast = 'True')

Group by FoodItem

Select @.FoodItems

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

|||

Thanks Jens, That works great and I did use it, seems faster. I am still at a loss as to why the fetch was returning a duplicate record but I like this better.

Larry

Monday, March 12, 2012

Fastest way to determine if a query returns any rows

Hi,
I hope I don't embarres myself because of the simplicity of this questrion,
but what would be the fastest way to determine, if a query would return any
rows (without actually returning them, and I don't need to know how many
there are, either)?
Thanks,
KlausYou can use the EXISTS() clause like:
IF EXISTS( SELECT * FROM ... )
Anith|||Thanks!
"Anith Sen" <anith@.bizdatasolutions.com> schrieb im Newsbeitrag
news:OOgsG%23LMFHA.3296@.TK2MSFTNGP15.phx.gbl...
> You can use the EXISTS() clause like:
> IF EXISTS( SELECT * FROM ... )
> --
> Anith
>

Fastest way to determine if a query returns any rows

Hi,
I hope I don't embarres myself because of the simplicity of this questrion,
but what would be the fastest way to determine, if a query would return any
rows (without actually returning them, and I don't need to know how many
there are, either)?
Thanks,
Klaus
You can use the EXISTS() clause like:
IF EXISTS( SELECT * FROM ... )
Anith
|||Thanks!
"Anith Sen" <anith@.bizdatasolutions.com> schrieb im Newsbeitrag
news:OOgsG%23LMFHA.3296@.TK2MSFTNGP15.phx.gbl...
> You can use the EXISTS() clause like:
> IF EXISTS( SELECT * FROM ... )
> --
> Anith
>

Fastest way to determine if a query returns any rows

Hi,
I hope I don't embarres myself because of the simplicity of this questrion,
but what would be the fastest way to determine, if a query would return any
rows (without actually returning them, and I don't need to know how many
there are, either)?
Thanks,
KlausYou can use the EXISTS() clause like:
IF EXISTS( SELECT * FROM ... )
--
Anith|||Thanks!
"Anith Sen" <anith@.bizdatasolutions.com> schrieb im Newsbeitrag
news:OOgsG%23LMFHA.3296@.TK2MSFTNGP15.phx.gbl...
> You can use the EXISTS() clause like:
> IF EXISTS( SELECT * FROM ... )
> --
> Anith
>

Sunday, February 26, 2012

False error when trying to return data in datagrid

VB.NET 2003 / SQLS2K

The Stored Procedure returns records within Query Analyzer.
But when the Stored Procedure is called by ADO.NET ~ it produced the
following error message.

--------
Exception Message: Failed to enable constraints. One or more rows
contain values violating non-null, unique, or foreign-key constraints.
--------

--------
Exception Source: System.Data
--------

If I click OK past the error messages I will get data filling the
datagrid. However not as I would like to see it.

Even though it returns the proper data rows and includes all the
columns I asked for, it also returns plenty of columns I didn't ask for
(all the columns of the main table) and all those columns are filled
with "null"

In addition each row header contains a red exclaimation mark whch when
hovered over reads;

"Column 'cmEditedBy' does not allow DBNull.Values."

An interesting thing about this column 'cmEditedBy' is that there is
noting wrong with it and all rows for that column contain data.

I believe this error is a mistake! But it probably indicates some other
problem. How should I track its cause?

M O R E ...
Below is the code in the data layer, the stored procedure, and the data
returned within query analyzer.

\\
'DataAdapter
Friend daView041CmptCyln As New SqlDataAdapter

'SqlCommand
Private daView041CmptCyln_CmdSel As New SqlCommand

'Add the command
daView041CmptCyln.SelectCommand = daView041CmptCyln_CmdSel

'Select
With daView041CmptCyln_CmdSel
.CommandType = CommandType.StoredProcedure
.CommandText = "usp_View_041Cmpt_ByJobCyln"
.Connection = sqlConn
With daView041CmptCyln_CmdSel.Parameters
.Add(New SqlParameter("@.RETURN_VALUE", SqlDbType.Int, _
4, ParameterDirection.ReturnValue, False, CType(0,
Byte), _
CType(0, Byte), "", DataRowVersion.Current, Nothing))
'Criteria
.Add("@.fkJob", SqlDbType.Text).Value = _
"48c64a55-874d-40d0-addc-7245f5d9c118"
'.Add("@.fkJob", SqlDbType.Text).Value = f050View.jobID
End With
End With
//

\\
ALTER PROCEDURE usp_View_041Cmpt_ByJobCyln
(@.fkJob char(36))
AS SET NOCOUNT ON;

SELECT
JobNumber,
DeviceName,
ComponentName,

Description,
Quan,
Bore,
Stroke,
Rod,
Seconds,
CylPSI,
PosA,
PosB,
PosC,
PosD,
PosE,
HomeIsRet,
RetIsRetrac,
POChecks,
Regulated,
FlowControl,
PortSize,
LoadMass

FROM tbl040cmpt
INNER JOIN tbl030Devi ON fkDevice = pkDeviceId
INNER JOIN tbl020Proc ON fkProcess = pkProcessId
INNER JOIN tbl010Job ON fkJob = pkjobId
INNER JOIN lkp202ComponentType ON fkComponenttype = pkComponentTypeId
INNER JOIN lkp201DeviceType ON fkDeviceType = pkDeviceTypeId
INNER JOIN lkp101PortSize on cmSmallint05 = pkPortSizeId

WHERE
(fkJob = @.fkJob)
--fkJob = '48c64a55-874d-40d0-addc-7245f5d9c118'
AND fkComponentType = 2

GO
//

(note - columns are wrapped)
\\
F1111Clip DriverCylinderClip Driver_2 - Top -
Cylinder91.2502.250.8752.250NULL01101110011/8 NPTNULL
F1111Punch MechCylinderPunch Mech_1 -
Cylinder_222.1002.0001.0001.234NULL11000110011/8
NPTNULL
F1111Clip
DriverCylinderBottom92.1002.0001.0001.000NULL11010110011/4
NPTNULL
F1111Punch MechCylinderPunch Mech_1 -
Cylinder_122.1002.0001.0001.000NULL01000110011/8
NPTNULL
F1111DegateCylinderDegate 1 -
Cylinder21.1882.500.8751.000NULL11000110011/8 NPTNULL
F1111Clip DriverCylinderClip Driver 1 -
Bottom11.1801.250.8751.000NULL00011110011/4 NPTNULL
//dbuchanan (dbuchanan52@.hotmail.com) writes:
> VB.NET 2003 / SQLS2K
> The Stored Procedure returns records within Query Analyzer.
> But when the Stored Procedure is called by ADO.NET ~ it produced the
> following error message.
> --------
> Exception Message: Failed to enable constraints. One or more rows
> contain values violating non-null, unique, or foreign-key constraints.
> --------

Nah, it sounds as if that message is produced by .Net Framework. The
stored procedure pleads innocense.

> Even though it returns the proper data rows and includes all the
> columns I asked for, it also returns plenty of columns I didn't ask for
> (all the columns of the main table) and all those columns are filled
> with "null"
> In addition each row header contains a red exclaimation mark whch when
> hovered over reads;
> "Column 'cmEditedBy' does not allow DBNull.Values."

Well, that column is not in the result set, so obviously when you try
to populated the DataSet, NULL values is all you get. And apparently
they are not permitted.

I don't have that much experience of ADO .Net, but it sounds to me that
you have run some wizard that has constructed your dataset, and you then
have not been careful which columns to include. (Personally, if I were
to work with data sets, I would probably construct them manually.) Or is
there some thought behind of including columns that are not reported by the
query?

While not relevant to your problem, permit me to point an issue of style
with your query: you table includes six tables, no column is prefixed
with any alias (or the table name). This makes it very difficult for
anyone who looks at query to tell which table, the columns are coming
from. This also mean that if the DBA adds, say, "Description" to one
more table, the procedure will no longer compile because that column name
is now ambiguous.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp