Monday, March 12, 2012
Help with Query
TableName: Customers
ID - Integer
CustomerID - VarChar
Name - VarChar
LoanNo= Int
Amount = Money
ID CustomerID Name LoanNo Amount
1 Chuck1 Chuck 1 2.00
2 Mike1 Mike 1 4.00
3 Dinah1 Dinah 1 6.00
4 James1 James 1 1.00
5 James1 James 2 3.00
6 Chuck1 Chuck 2 5.00
What I want to do is return all the Customers but only their highest LoanNo
(like MAX(LoanNo)).
Example:
ID CustomerID Name LoanNo Amount
2 Mike1 Mike 1 4.00
3 Dinah1 Dinah 1 6.00
5 James1 James 2 3.00
6 Chuck1 Chuck 2 5.00
Thanks,
Chuck> What I want to do is return all the Customers but only their highest
> LoanNo
> (like MAX(LoanNo)).
One method is with a derived table. Untested example:
SELECT
Customers.ID,
Customers.Name,
Customers.LoanNo,
Customers.Amount
FROM Customers
JOIN
(SELECT
CustomerID,
MAX(LoanNo) AS LoanNo
FROM dbo.Customers
GROUP BY CustomerID) AS MaxLoanNos ON
MaxLoanNos.CustomerID = Customers.CustomerID AND
MaxLoanNos.LoanNo = Customers.LoanNo
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:eBja%23rl7GHA.3760@.TK2MSFTNGP02.phx.gbl...
>I have the following table:
> TableName: Customers
> ID - Integer
> CustomerID - VarChar
> Name - VarChar
> LoanNo= Int
> Amount = Money
> ID CustomerID Name LoanNo Amount
> 1 Chuck1 Chuck 1 2.00
> 2 Mike1 Mike 1 4.00
> 3 Dinah1 Dinah 1 6.00
> 4 James1 James 1 1.00
> 5 James1 James 2 3.00
> 6 Chuck1 Chuck 2 5.00
> What I want to do is return all the Customers but only their highest
> LoanNo
> (like MAX(LoanNo)).
> Example:
> ID CustomerID Name LoanNo Amount
> 2 Mike1 Mike 1 4.00
> 3 Dinah1 Dinah 1 6.00
> 5 James1 James 2 3.00
> 6 Chuck1 Chuck 2 5.00
> Thanks,
> Chuck
>
Friday, February 24, 2012
Help with math in function
Here is the code:
create or replace function readip(unsint number)
return varchar
is
ip_address varchar(15);
a number(3);
b number(3);
c number(3);
d number(3);
begin
a := mod( ( unsint / 16777216 ), 256);
b := mod( ( unsint / 65536 ), 256);
c := mod( ( unsint / 256 ), 256);
d := mod( ( unsint ), 256);
ip_address := a||'.'||b||'.'||c||'.'||d;
return ip_address;
end readip;
Thank you much!
MonicaIf you want the decimals to be displayed, define the variables as
a number(10,3);
Hope this helps.|||Originally posted by DBW-Monica
I have a function that reads an unsigned integer and translates it into an IP address. The problem is that it is rounding the numbers so the IP numbers are off by 1 either up or down, i.e., 144.xxx.xxx.xx will show as 145.xxx.... or 144.456.xx... will show as 144.455.xx....Can someone help?
Here is the code:
create or replace function readip(unsint number)
return varchar
is
ip_address varchar(15);
a number(3);
b number(3);
c number(3);
d number(3);
begin
a := mod( ( unsint / 16777216 ), 256);
b := mod( ( unsint / 65536 ), 256);
c := mod( ( unsint / 256 ), 256);
d := mod( ( unsint ), 256);
ip_address := a||'.'||b||'.'||c||'.'||d;
return ip_address;
end readip;
Thank you much!
Monica
must be missing the obvious, but I just don't get it. Can you give an example of an input value, and what your function should return ?
Sunday, February 19, 2012
Help with large text fields please
A modest table with several large fields (currently varchar(5000)), plus some datetime and integer fields recording who's done what and when.
Two problems - (1) I now realise that I'm limited to 8060 characters, and (2) users seem to think even 5000 chars might be too small on occasions (the table is for recording laboratory problems, so the amount of text depends on what the local quality manager finds!)
I thought I'd change my varchars to text. However, when I changed just one of them to text, the record set being returned by my stored procedure has lots of empty fields. The query ran OK before the datatype change and STILL runs OK in Enterprise Manager after the change.
The basic query is:
Code: ( text )
SELECT TechAnomalies.*,
lab1.LabRef AS labref1, lab1.LabName AS labname1, lab1.EULabRef AS EULabRef,
lab2.LabRef AS labref2, lab2.LabName AS labname2,
u1.UserFullName AS RaisedBy,
u2.UserFullName AS Inter,
u3.UserFullName AS SignOffBy,
u4.UserFullName AS LastEditor,
u5.UserFullName AS LQM
FROM dbo.TechAnomalies
INNER JOIN dbo.Labs AS lab1
ON lab1.LabID = TechAnomalies.TALabID
LEFT OUTER JOIN dbo.Labs AS lab2
ON lab2.LabID = TechAnomalies.TAIntermedLabID
LEFT OUTER JOIN dbo.Users AS u1
ON u1.UserID = TechAnomalies.TARaiserUserID
LEFT OUTER JOIN dbo.Users AS u2
ON u2.UserID = TechAnomalies.TAIntermedUserID
LEFT OUTER JOIN dbo.Users AS u3
ON u3.UserID = TechAnomalies.TASignedOffBy
LEFT OUTER JOIN dbo.Users AS u4
ON u4.UserID = TechAnomalies.TALastEditedBy
LEFT OUTER JOIN dbo.Users AS u5
ON u5.UserID = TechAnomalies.TALQMReviewBy
I use it with or without a WHERE clause (passed to the stored procedure as a varchar) to return either a recordset or the details of one record.
Any suggestions please?
You could try using nvarcharhelp with integer field
i have an autonumber field (primary key) and another integer field as part of a table. What i want to do is when a record is created, the default value of the integer field should be the_autonumber+1000 for eg record with pk 82 will have an integer field that's automatically 1082. Would it be possible to do this ? Thanks in advance.Hi, try this trigger...
CREATE TRIGGER trig_MyTableAddTrigger
ON MyTable
FOR INSERT
AS
-- Get the value to put in the second column
DECLARE @.NewValue integer
SELECT @.NewValue = (SELECT MyTableID FROM Inserted) + 1000
-- Update the same table [MyTable] with new value
UPDATE [MyTable] SET SecondColumName = @.NewValue
WHERE MyTableID = (SELECT MyTableID FROM INSERTED)|||thank you very,very much for your help.
Help with inserting a new record into database - error Must declare the scalar variable "@
Hi,
Can anybody help me with this, I've got a simple program to add a new record to a table (2 items ID - Integer and Program - String) that matches all examples I can find, but when I run it I get the error :
Must declare the scalar variable "@.BookMarkArrayA".
when it reaches the .insert command, I've tried using a local variable temp in place of the array element and.ToString , but still get the same error
This is the code :
PublicSub NewCustomer()
Dim tempAsString =" "
Dim IDAsInteger = 1
'Restore the array from the view state
BookMarkArrayA =Me.ViewState("BookMarkArrayA")
temp = BookMarkArrayA(6)
Dim CustomerAs SqlDataSource =New SqlDataSource()
Customer.ConnectionString = ConfigurationManager.ConnectionStrings("CustomerConnectionString").ToString()
Customer.InsertCommand ="INSERT INTO [Table1] ([ID],[Program]) VALUES (@.ID, @.BookMarkArrayA(6))"
Customer.InsertParameters.Add ("ID", ID)
Customer.InsertParameters.Add ("Program",@.BookMarkArrayA(6))
Customer.Insert()
EndSub
Cheers
Ken
I'm not sure where you got the (6) syntax from?
Try this. Change these 3 lines:
Customer.InsertCommand ="INSERT INTO [Table1] ([ID],[Program]) VALUES (@.ID, @.BookMarkArrayA(6))"
Customer.InsertParameters.Add ("ID", ID)
Customer.InsertParameters.Add ("Program",@.BookMarkArrayA(6))
to this and you should have better luck:
Customer.InsertCommand ="INSERT INTO [Table1] ([ID],[Program]) VALUES (@.ID, @.BookMarkArrayA)"
Customer.InsertParameters.Add ("@.ID", SqlDbType.Integer).Value = ID
Customer.InsertParameters.Add ("@.Program", SqlDbType.VarChar,6).Value =@.BookMarkArrayA)
|||Terri,
Thanks for the reply, it didn't compile as the SqlDbType wasn't recognised.
The origional error was @.BookMarkArray was an undeclared scalar variable, which suggests to me that this part of the command requires a pointer to the actual variable BookMarkArray denoted by putting the @. symbol first.
The (6) syntak by the way, was the element of the array I wanted to load into the DB.
The code fits with other examples I've looked up, I'm proberbly missing something simple.
Ken
|||Boy did I screw up the code.Customer.InsertCommand ="INSERT INTO [Table1] ([ID],[Program]) VALUES (@.ID, @.Program)"
Customer.InsertParameters.Add ("@.ID", SqlDbType.Int).Value = ID
Customer.InsertParameters.Add ("@.Program", SqlDbType.VarChar,6).Value = BookMarkArray(6)
|||Terri,
I still get the error that SqlDbType is not declared, am i missing inheriting a library or some thing
Ken
|||Well, try qualifying it by adding the namespace in front and see if that takes care of it.SqlClient.SqlDbType.Int|||
Terri,
I get the error SqlClient not declared, so I assume that I'm not inheriting something.
I tried the following code which worked, but I can't find a method to change the String value 'Test Program' into a parameter:
Dim sqlConnection1AsNew System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("CustomerConnectionString").ToString())Dim cmdAsNew System.Data.SqlClient.SqlCommandWith cmd.CommandType = System.Data.CommandType.Text
.CommandText =
"INSERT Into Customer (Program) VALUES ('Test Program')".Connection = sqlConnection1
EndWithsqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
Ken
|||Try that code block like this (updates in pink):Dim sqlConnection1AsNew System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("CustomerConnectionString").ToString())Dim cmdAsNew System.Data.SqlClient.SqlCommandWith cmd|||.CommandType = System.Data.CommandType.Text
.CommandText = "INSERT Into Customer (Program) VALUES (@.Program)"
.Connection = sqlConnection1
EndWithcmd.Parameters.Add("@.Program", System.Data.SqlDbType.VarChar, 99).Value = "Test Program"
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
Terri,
Thanks, that worked, althrough I'd tried variations of adding parameters before without success.
My concern is that I don't seem to be able to see the namespace System.Data.SQLClient and when you look at the methods, it says you must reference this namespace but don't tell you how.
Ken
|||
KenWalker:
My concern is that I don't seem to be able to see the namespace System.Data.SQLClient
If you are using code inline (ie, not a separate .vb file), put this at the top of the page, right below the @.Page directive:
If you are using code beside/behind, put this at the very top of your code:<%@. Import Namespace="System.Configuration" %>
|||Imports System.Data.Client
I'm lazy, so I put it in web.config under the system.web section:
<pagestheme="default">
<namespaces>
<addnamespace="System.Data"/>
<addnamespace="System.Data.SqlClient"/>
<addnamespace="System.Configuration.ConfigurationManager"/>
</namespaces>
</pages>
|||
Thanks Terry, I have solved the problem using -Dim cmdAsNew System.Data.SqlClient.SqlCommand and then adding the parameters that way.
Ken