Showing posts with label solve. Show all posts
Showing posts with label solve. Show all posts

Thursday, March 22, 2012

DISTINCT COLOUR, BUT MUST HAVE PRIM KEY

So how do i solve this sql statment

Select distinct colour from wwwstocktbl where colour <> ' ' order by colour

The stament works as a statment but when run in asp i get an error as theres no primary key, but if i add the primary key then the select distinct fails

Select distinct ProductCode, Colour from wwwstocktbl where colour <> ' ' order by colourThe code as written should work correctly. Are you trying to use this code in a data adapter? We are going to need to see some of hte asp.net code in order to be able to help you.

Terri|||SqlSelectCommand4.CommandText = "select DISTINCT COLOUR from wwwstocktbl order by COLOUR"
SqlDataAdapter4.Fill(DataSet21, "wwwstocktbl")
DropDownListMaxPrice = DataBinder.Eval(DataSet21, "Tables[wwwStockTBL].DefaultView.[0].COLOUR")
DropDownListMinPrice.DataBind()

the problem is that to achive the distinct colour you must exclude the ProductCode field, but doing that generates an error as there is no primary key

<<<<---code copied from error page->>>>>>>
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 231:
Line 232: SqlSelectCommand4.CommandText = "select DISTINCT COLOUR from wwwstocktbl order by COLOUR"
Line 233: SqlDataAdapter4.Fill(DataSet21, "wwwstocktbl")
Line 234: DropDownListColour = DataBinder.Eval(DataSet21, "Tables(wwwStockTBL).DefaultView.(0).COLOUR")
Line 235: DropDownListColour.DataBind()

<<<<---end code copied from error page->>>>>>
chnaging the line
SqlSelectCommand4.CommandText = "select DISTINCT ProductCode, COLOUR from wwwstocktbl order by COLOUR"

would give a very differant set of results

???? do you think there are two problems here
1 the error being generated and
2 the code needed for the distinct cluse?|||Well, think about it. If you have BLUE in the table 5 times with 5 different codes, and the user chooses BLUE from the dropdownlist, WHICH BLUE is the database supposed to be using?

If you don't particularly care which BLUE your query chooses, then you can use a query like this:


SELECT MAX(ProductCode) AS ProductCode, Colour FROM wwwstocktbl GROUP BY Colour ORDER BY Colour

This will give you a resultset of unique colours along with the MAX(ProductCode) for each.

Terri|||excelant, that code was realy interesting

after you asked about was i useing a datset i woundered why, so i created a dataset and dataadapter just for the colour question and it worked, but why, in my first sql code tests i selected all the fields, then in the finnal run just the colour and got the error, i then tryed to "dim" a new dataadapter and dataset at run time but with no sucsess, is there some rule about being able to change the fields returned by say a second sql statment such as at design time i define 3 fields but then at run time only request 2?

Wednesday, March 21, 2012

distibuted transations in linked server

please solve this problem.

when i am executing any transaction from one server to update record in other linked server database.

create PROCEDURE [dbo].[aTest]

@.iNum as int

AS

BEGIN

SET NOCOUNT ON;

begin tran

update aaa set sName='Deepak'

where iNum=@.iNum

exec [REPORTSRVR\SQL2005].HDFCCUSTOMER.dbo.aProc @.iNum

if @.@.error <> 0

begin

rollback tran

return

end

else

begin

commit tran

end

SET NOCOUNT OFF;

END

it shows following error

The operation could not be performed because OLE DB provider "SQLNCLI" for linked server "REPORTSRVR\SQL2005" was unable to begin a distributed transaction.

1. Check/change your server option "remote proc trans" via sp_configure or Connections tab in SQL Server Instance Properties window

2. If you need MS DTC for distributed transactions: http://blogs.msdn.com/mab/archive/2005/12/30/508273.aspx

|||

thanks for response.

i m using transaction statement. is it possible that single transaction can talk from two server?

Sunday, February 19, 2012

display only the listing with the lowest price (was "Need a little help here....

I have a little problem that I just haven't been able to solve. I don't think it is very difficult but I can't seem to make it work. Here's the scenario:

I have a database with the following values:
Model Make Price
DA1100 GTN $88.00
DA1100 GTN $100.00
DA1000 GBN $110.00
DA668 GTN $100.00
DA880 GTN $200.00

In this case DA1100 is listed twice with 2 different prices. I only want to display the one with the lowest price. So the result I want is:
Model Make Price
DA1100 GTN $88.00
DA1000 GBN $110.00
DA668 GTN $100.00
DA880 GTN $200.00select Model,
Make,
min(Price) as MinPrice
from UrTableName
group by Model,
Make