|
I need some help with a stored procedure, I have a table in my database that I am writing records to, specifically a BeginningDate field and EndingDate field. I want to be able to run a stored procedure that selects records based on a range of dates. This range of dates begins with monday and ends the following sunday, it covers an entire week. When I run the stored proc the where clause goes something like this:
<br />
select<br />
UserID,<br />
BeginningDate,<br />
EndingDate,<br />
<br />
From tableName<br />
<br />
where UserID = @UserID and BeginningDate >= @BeginDate and EndingDate <= @EndDate<br />
<br />
I know that it will not return any records if there is a date that is less than the EndDate. My question is how can I adjust this so it selects the one record based on a BeginDate and EndDate parameters?
|
|
|
|
|
select
UserID,
BeginningDate,
EndingDate,
From tableName
where UserID = @UserID and ((BeginningDate >= @BeginDate and EndingDate <= @EndDate) OR (BeginningDate >= @BeginDate) OR (EndingDate <= @EndDate))
Hope thats what you want ?
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
coolestCoder
|
|
|
|
|
Hi, i have data like this in temp table
Owner - DocumentPath
1 - c:\reports\fileA
1 - c:\reports\fileB
1 - c:\reports\fileC
2 - c:\reports\temp\FileA
2 - c:\reports\fileA
2 - c:\reports\fileB
AND SO ON....
i would like to know...
Is it possible to concatenate all DocumentPath column's into single column for each Owner ?
so i would have a new temp table that looks like this (if possible... this is still theoretical)
Owner - DocumentPaths
1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC
2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB
AND SO ON....
The reason i want to do this is because i need all the file paths in one line in one field on a report later... I thought i could PIVOT the data and then concatenate them...
OR
Is it possible to select only the top 3 (ordered by Modified Date DESCENDING) for each Owner ???
like:
SELECT TOP 3 Owner, DocumentPath<br />
FROM #tempTableDocPath<br />
ORDER BY Owner ASC,Modified DESC<br />
BUT for each owner show top 3 (if they have three)
AND then Is it possible ONCE AGAIN to CONCATENATE the DocumentPath column into one column (in a new table / tempTable)?
like this:
Owner - DocumentPath
1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC
2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB
AND SO ON....
Thank you in advance. I hope i put it all down clearly
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
modified on Wednesday, December 05, 2007 9:19:40 AM
|
|
|
|
|
Hi,
1. You can always write a stored procedure, wherein you can use cursor to concatenate the output of some SQL query. This string can then be returned / stored in some temporary table, which will be used later.
Support123 wrote: Is it possible to select only the top 3 (ordered by Modified Date DESCENDING) for each Owner???
like:
SELECT TOP 3 Owner, DocumentPath
FROM #tempTableDocPath
ORDER BY Owner ASC,Modified DESC
BUT for each owner show top 3 (if they have three)
AND then Is it possible ONCE AGAIN to CONCATENATE the DocumentPath column into one column (in a new table / tempTable)?
like this:
Owner - DocumentPath
1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC
2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB
AND SO ON....
I didnt got this. Can you please clarify in details, what exactly you want in this case ??
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
coolestCoder
|
|
|
|
|
i want to put all the rows where the owner is the same (example 1... owner 1 is repeated three times because he has 3 documents linked to him... so he would look like this in the db:
Owner - DocumentPath
1 - c:\reports\fileA
1 - c:\reports\fileB
1 - c:\reports\fileC)
what i want to do is take all the paths linked to this owner, and put them into one column... to look like this.
Owner - DocumentPath
1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC
So you see, now i have all the values needed in one column. This makes it TONS easier to display on a report.
So Column 1 (Owner) would have the Owner id and the Column 2 (DocumentPath) will have all the DocumentPaths found linked to the owner in column 1. I hope this is more clear???
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
Support123 wrote: I hope this is more clear???
Yes, it is clear now.
Can you provide some table structure for a more precise answer ?
It would help me allot.
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
coolestCoder
|
|
|
|
|
ALTER PROCEDURE dbo.SelectData2<br />
(<br />
@naam1 varchar(30),<br />
@naam2 varchar(30),<br />
@leeftijd int,<br />
@numberfound int OUTPUT<br />
)<br />
AS<br />
SET NOCOUNT OFF<br />
SELECT ID<br />
FROM vrienden<br />
WHERE Voornaam = @naam1 AND Achternaam = @naam2<br />
SET @numberfound = @@ROWCOUNT<br />
IF (@numberfound = 0)<br />
BEGIN<br />
INSERT INTO vrienden (Voornaam, Achternaam, Leeftijd)<br />
VALUES (@naam1, @naam2, @leeftijd)<br />
END<br />
IF (@numberfound > 0) <br />
BEGIN<br />
UPDATE vrienden<br />
SET Leeftijd = @leeftijd<br />
WHERE Voornaam = @naam1 AND Achternaam = @naam2<br />
END<br />
RETURN
I entered this Stored Procedure in VWD and it accepted it, however I wonder if it is possible to do a SELECT and an INSERT or UPDATE in the same procedure.
My question is, would this work? Or should I make two stored procedures one to find if a certain entry is already in the database and a second stored procedure to change the database?
Also I am not sure yet how to process an OUTPUT variable.
Please some advice.
Thanks! Ranger49
|
|
|
|
|
A stored procedure can contain as many CRUD operations as you like. However you should consider giving your stored procedures more meaningful names. For instance, your SP is called SelectData2 but it performs select, insert and update operations. Ideally, the name of the stored procedure should indicate its purpose.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
pmarfleet wrote: A stored procedure can contain as many CRUD operations as you like. However you should consider giving your stored procedures more meaningful names. For instance, your SP is called SelectData2 but it performs select, insert and update operations. Ideally, the name of the stored procedure should indicate its purpose.
You are right, but this was only my second attempt ever, so I called it SelectData2.
Would you know of an example where a stored procedure is called with OUTPUT parameter? I figure my attempt failed due to an error in the calling cs file, and that the Stored Procedure itself is fine.
I Googled it, and got some examples, but they weren't answering my question.
Ranger49
|
|
|
|
|
When you create the Sql parameter for the output parameter, you set its direction to Output or InputOutput. When you say it failed, what do you mean? Did you get an exception or did it not behave as you would expect?
|
|
|
|
|
Hi,
Can I connect with visual basic.net to a online server (I have a space on the server with my hosting company, the server has mySQL database on it.)?
If I can please tell me how (connection string, methods to use, namespaces etc...)
The idea was to totally work online using mySQL server?
Thanks.
|
|
|
|
|
You can and this[^] lists the connection strings you can use. Install the native MySql .NET provider and use this in your code. There are plenty of good tutorials for using this online.
|
|
|
|
|
Hi,
Thanks for your reply...
my problem is
iam using (MS-Access) database
ihave to store multiple items in one invoice number(primary)
ihave to design table like below format...
invoiceno items units
1 nokia1600 2
nokia6600 3
nokia6633 4
2 nokia1600 2
nokia1400 3
nokia1500 6
3 nokia1800 5
nokia1900 6
like this ....(database design) and coding in c# application
murali krishna
|
|
|
|
|
You will have a master table as you have shown already and details table referencing invoice number (primary key) of the master table. Then you can add as many records against your primary as you want.
Your query will include an inner join with condition -- MasterTable.InvoiceNo = DetailsTable.InvoiceNo
Hope that answers your question.
You must be able to complete rest of the work.
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
coolestCoder
|
|
|
|
|
Hi,
my database is Ms-Access database..
iam doing product based application(windows application)
i have to store data based on the invoice number...
invoice number is (primarykey)
based on invoice number i have to store multiple items
reports
when i select invoice number those items should be displayed...
please help me
murali krishna
|
|
|
|
|
Hi,
Just write the query with where condition.
for eg.
select * from tablename where invoicenumber='xyz'
Regards,
Ujjaval Modi
|
|
|
|
|
Hi
Thanks for your reply...
my problem is
my database is (ms-access) database
ihave to store multiple items in one invoice number(primary)
invoiceNo items units
1 Nokia6600 4
Nokia6633 5
Nokia6030 3
2 Nokia 1300 2
Nokia 1100 3
like this ....(database design) and coding in c# application
murali krishna
|
|
|
|
|
What exactly do u want ?
A database design or u already have one ?
Regards,
Ujjaval Modi
|
|
|
|
|
Hi,
yes i want database design
murali krishna
|
|
|
|
|
Just mention clearly that u want a working sample.
Regards,
Ujjaval Modi
|
|
|
|
|
Hi,
ya if it is ok ----
clearly i want database design and query for inserting and selecting
that that particular table
i have to desing table like this
invoiceNo(primarykey),products,units,Cost
in this one invoice number should consists od mmultiple products information....
this is main i Want ..
murali krishna
|
|
|
|
|
i think your datatable should designed as follows:
ProductedId(pk) Products units TypeId
1 Nokia1600 2 1
2 Nokia1700 5 1
3 Nokia1800 6 1
4 Nokia2600 2 2
5 Nokia4600 8 2
6 Nokia8100 2 3
7 Nokia8300 1 3
8 Nokia56 6 3
9 Nokia1310 2 3
Connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
query sentence:
select * from products where typeid=x;
hope it will work as you wanted.
modified on Saturday, December 29, 2007 11:57:04 PM
|
|
|
|
|
I want to trim the value so i just write a simple query
in MSACCESS 2000
SELECT Trim([ItemName]) AS Expr1
FROM Items
But it will give error "Undefind function "Trim" in expression"
What to do...?
Regards
Pankaj Joshi
If you want to shape your dreams into reality, please wake-up...
|
|
|
|
|
There is no TRIM commmand in T-SQL you have to use LTRIM then RTRIM as follows:
SELECT LTRIM(RTRIM(ItemName)) Expr1 FROM Items
Regards
Guy
You always pass failure on the way to success.
|
|
|
|
|
Thanks for reply but still query is not working
Same error occurred for LTRIM function
SELECT LTRIM(RTRIM(ItemFullName)) as Expr1 FROM Items
Regards
Pankaj Joshi
If you want to shape your dreams into reality, please wake-up...
|
|
|
|