Help, how to make a query? I need from the List to add the new data in table 1: category, date, description, and category (category id). But the text of the category want to find in the other table name of Category and add in table 1 id this category.
Can't properly write the query. Help please.
Loading
Zoran HorvatPosted Jul 13, 2011, 7:34 AM
ALTER PROCEDURE AddNewAmount
@amount real,
@entryDate date,
@description nvarchar(1000),
@category nvarchar(1000)
AS
INSERT INTO MoneyEntries (Amount, EntryDate, Description, Category)
SELECT @amount, @entryDate, @description, CategoryID
FROM Category
WHERE Category=@Category
I have changed categoryID argument into category description. But if you do in this way, then you must make sure that category with specified description exists in the database and that it is unique.
Otherwise, MoneyEntry class should be changed to contain category identity as well.
Another scenario is to create new category if it does not exist in the database and then to use newly acquired identity to insert money entry.
Zoran
Mike JonsonPosted Jul 13, 2011, 9:38 AM
Zoran HorvatPosted Jul 13, 2011, 8:44 AM
Zoran
Mike JonsonPosted Jul 13, 2011, 8:35 AM
Jaganathan BantheswaranPosted Jul 13, 2011, 7:43 AM
I cant understand your question.. Could you explain clearly based on your last post update
Mike JonsonPosted Jul 13, 2011, 7:28 AM
@amount real,
@entryDate date,
@description nvarchar(1000),
@categoryID int,
@nameCategory nvarchar(100)
Insert InTo MoneyEntries (Amount, EntryDate, Description, Category) Values (@amount, @entryDate, @description, @CategoryID = ID)
Where
Select ID From Category Where Name = nameCategory
But i think will not work
Mike JonsonPosted Jul 13, 2011, 7:09 AM
public List
{
get { return _entries; }
set { _entries = value; }
}
public class MoneyEntry
{
// ????????? ????????(????? ??? ??????)
private double _amount;
public double Amount
{
get { return _amount; }
set { _amount = value; }
}
//???? ????
private DateTime _date;
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
//Category
private string _category;
public string Category
{
get { return _category; }
set { _category = value; }
}
//???????? ?????????? ????????
private string _description;
public string Description
{
get { return _description; }
set { _description = value; }
}
I use for add foreach(MoneyEntry myList in this._entries) { ... }
Zoran HorvatPosted Jul 13, 2011, 6:56 AM
Generally, when populating the list, you should show the category name but preserve the corresponding category ID values so that they can be used to call this procedure which inserts row back into the database.
Zoran
Mike JonsonPosted Jul 13, 2011, 6:51 AM
ALTER PROCEDURE AddNewAmount
@amount real,
@entryDate date,
@description nvarchar(1000),
@categoryID int
AS
Insert InTo MoneyEntries (Amount, EntryDate, Description, Category) Values (@amount, @entryDate, @description, @CategoryID)
But in my List category its string type and need found this categoryName in Table Category and than add id this category in Table MoneyEntries
Zoran HorvatPosted Jul 13, 2011, 6:49 AM
INSERT INTO Table1(Category, Date, Description, CategoryId)
SELECT 'Category descrption', '07/13/2011', 'Some description', CategoryId
FROM Category
WHERE Category='Category descrption'
Zoran
Zoran HorvatPosted Jul 13, 2011, 6:43 AM
Zoran