I’m trying to figure out the best way to add multiple rows into an existing SQL table using ASP.NET. I'm new to VBA programming so am a little lost and need some help with this problem.
Example, the user will enter.
2 (LotNo)
5 (itemNo) – Add multiple rows depending on this value.
100 (cartNo)
20120202 (Date)
Result:
2, 1, 100, 20120202
2, 2, 100, 20120202
2, 3, 100, 20120202
2, 4, 100, 20120202
2, 5, 100, 20120202
You can try with this code - based on Table Value Parameter
Dim sc As New SqlCommand(
"INSERT INTO MyNewTable (field1, field2,...)"&
"SELECT field1, field2,... FROM #MyTable;", connection)
sc.Parameters.AddWithValue("#MyTable", MyTable)
sc.ExecuteNonQuery()
Link :http://msdn.microsoft.com/en-us/library/bb510489.aspx
Related
I need to take the last 7 values of a column I was thinking of using 'LIMIT' but I don't know if it also works in reverse. Take an example, I have these values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; I need to get 4, 5, 6, 7, 8, 9, 10, considering the fact that in the future other records will be added after 10. Is there a solution with 'LIMIT' or are there other solutions? I can't use ORDER BY 'couse the values i need get are the last 7 days of values and sqlite don't have date type and I don't registered date as milliseconds but i do in date format dd-mm-yyyy so use 'ORDER BY date' don't works. Thanks
If you change the format of the dates to YYYY-MM-DD then it is as simple as this:
SELECT columnname
FROM tablename
ORDER BY date DESC
LIMIT 7
As it is now the date format the only thing that you can do is construct the proper format in the ORDER BY clause with SUBSTR():
SELECT columnname
FROM tablename
ORDER BY SUBSTR(date, 7) || SUBSTR(date, 4, 2) || SUBSTR(date, 1, 2) DESC
LIMIT 7
This expression:
SUBSTR(date, 7) || SUBSTR(date, 4, 2) || SUBSTR(date, 1, 2)
transforms a date like 05-01-2020 to 20200105 so it is comparable and can be used to sort the rows.
I have a table as ITEM_LOC with the following format:
ITEM LOC UNIT RETAIL
100 KS 20
101 JS 22
102 RS null
I need to find the unit retail,
if the record if present for loc: RS, it should get that.
If it doesn't exist for RS, it should find for JS.
If it doesn't exist for JS, it should find for KS
and if it doesn't exist of KS, it should return NULL.
How do I go about this case ?
You could do this with a sub-select, that translates the LOC into an number ordered by priority, and which takes the best numeric value present:
SELECT *
FROM ITEM_LOC
WHERE decode(LOC, 'RS', 1, 'JS', 2, 'KS', 3, null) =
(
SELECT min(decode(LOC, 'RS', 1, 'JS', 2, 'KS', 3, 4))
FROM ITEM_LOC
)
Note that if none of these LOC codes exist, you will get an empty result set.
If there are duplicate codes, you can have more than one result.
If a column in the SELECT clause is omitted from the GROUP BY clause, does SQLite group by the remaining columns (by default), and then return the value of the omitted column in the first row it evaluates?
For example, finding the TransactionId associated with the highest value per ProductId:
CREATE TABLE IF NOT EXISTS ProductTransaction
(
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
ProductId INTEGER NOT NULL,
TransactionType INTEGER NOT NULL,
Value INTEGER NOT NULL
);
INSERT INTO ProductTransaction (ProductId, TransactionType, Value)
VALUES (1, 7, 23), (1, 3, 12), (2, 4, 43), (1, 7, 5), (1, 10, 23),
(3, 3, 23), (3, 2, 31), (1, 1, 23), (2, 5, 50), (2, 6, 14), (1, 4, 23);
SELECT ProductId
, TransactionType
, MAX(Value)
FROM ProductTransaction
GROUP BY ProductId;
DELETE FROM ProductTransaction;
Running the previous statements gives me the TransactionType of 7 for ProductId 1 (Highest value 23).
However, if I add an the index:
CREATE INDEX IF NOT EXISTS IDX_TransType ON ProductTransaction(ProductId ASC, TransactionType ASC);
It returns the TransactionType 1, presumably because it's now ordering the rows according to the index. Modifying the index supports this theory:
CREATE INDEX IF NOT EXISTS IDX_TransType ON ProductTransaction(ProductId ASC, TransactionType DESC);
It will now return TransactionType 10 for ProductId 1.
Is this behaviour by design, or is it just an unreliable side-effect?
EDIT: It seems that it's an unreliable side-effect. From the documentation:
Each expression in the result-set is then evaluated once for each
group of rows. If the expression is an aggregate expression, it is
evaluated across all rows in the group. Otherwise, it is evaluated
against a single arbitrarily chosen row from within the group. If
there is more than one non-aggregate expression in the result-set,
then all such expressions are evaluated for the same row.
https://www.sqlite.org/lang_select.html#resultset
Since SQLite 3.7.11, using MAX() or MIN() will force any non-aggregated columns to come from the same row that matches the MAX()/MIN().
However, when there are multiple rows with the same largest/smalles value, it is still unspecified from which of those rows the other columns' values come. (SQLite's behaviour is consistent in this regard, but can change in different versions or with different database schemas.)
My english won't help to explain what my problem is but i will give a try.
Lets say we have a table with (photoId, _bandId, desc)
Now we have a set of records
1, 1000, TestDesc1
2, 1000, TestDesc2
3, 1010, TestDesc3
4, 1900, TestDesc4
5, 1000, TestDesc5
Im trying to find a select command where gets all the [desc] for a specific [_bandId]
BUT AS FIRST RESULT i want a specific photoId
What i use,
SELECT * FROM [myTable]
WHERE _bandId = (SELECT _bandId from [myTable] where photoId=2)
This of course gives me as output :
TestDesc1
TestDesc2
TestDesc5
BUT what i really need as output is this
TestDesc2
TestDesc1
TestDesc5
Kind Regards
Konstantinos
Add an order by with a case as the first parameter, where you create a value that is 0 for the record that you want first and 1 for the other records. After that you can add more parameters for how you want to sort the remaing records, for example on description:
SELECT
*
FROM
[myTable]
WHERE
_bandId = (SELECT _bandId from [myTable] where photoId=2)
ORDER BY
CASE PhotoId WHEN 2 THEN 0 ELSE 1 END,
[desc]
I'm using Simple.Data and its InMemoryAdapter to write some tests.
The code below is from one of my tests. The test should result in 1 value "Atlanta" being returned, however I get Atlanta twice. I'm wondering if I'm using InMemoryAdapter of Distinct incorrectly?
var adapter = new InMemoryAdapter();
Database.UseMockAdapter(adapter);
var db = Database.Open();
db.ParentRegionList.Insert(Id: 1, RegionName: "Dublin");
db.ParentRegionList.Insert(Id: 2, RegionName: "Atlanta");
db.ParentRegionList.Insert(Id: 3, RegionName: "Atlanta");
db.ParentRegionList.Insert(Id: 4, RegionName: "Killarney");
db.ParentRegionList.Insert(Id: 5, RegionName: "Bournemouth");
var result = db.ParentRegionList.All()
.Where(db.ParentRegionList.RegionName.Like("At%"))
.Distinct()
.Select(db.ParentRegionList.RegionName).Take(10)
.ToScalarList<string>();
I've also tried:
var result = db.ParentRegionList.All()
.Where(db.ParentRegionList.RegionName.Like("At%"))
.Select(db.ParentRegionList.RegionName.Distinct()).Take(10)
.ToScalarList<string>();
I think the second format is correct, I had similar problem. Instead of using a cast as
ToScalarList<string>() try creating a dummy type, eg. "Region" that has a string property with name RegionName and cast as: ToList<Region>()