There is a table with the following columns:
ProdID
Specification
Value
I've got a csv file with a schema that only has the last two columns - Specification and Value. In other words they are my csv headers. Now I need to associate the values of this file against one unique id.
The significance of that id is that it is a reference to the document; its not important here, but, for now assume it is reqired by the system.
Now while I use the SqlBulkCopy class to insert is there a way that I can still insert some value for ProdID such that each row inserted will have the same ProdID for that file I am trying to bulk copy...?
That depends. if ProdID is an identity field in the database, the database will fill it for you when you perform the bulk copy.
If it's not, you will need to fill it yourself and pass the three columns to SqlBulkCopy, instead of just the two.
You'd have to do something on the lines of the following code:
string cmdTxt = "SELECT " + ProdID + ", * FROM [sample.csv]";
It doesn't matter where you place the ProdID field in the select clause above; in the beginning or end, you'll get the result set with ProdID as the first field...
Related
This question already has answers here:
Parse comma-separated string to make IN List of strings in the Where clause
(4 answers)
Closed 2 years ago.
I have the below column in table x where SharedBagsIds contains the string (1,3,4)
I am trying to write an SQL query that contains the in clause as follow
select * from .... where id in (x.SharedBagsIds)
but this line id in (x.SharedBagsIds) is generating an error
Conversion failed when converting the varchar value '1,3,4' to data type int.
is there a way to fix this issue?
This is not how the IN clause is used and you're comparing an integer (your id) to a string ('1,3,4'). You'll need to split the column value into multiple values, then check if the id matches any of the values:
WHERE (',' + RTRIM(SharedBagsIds) + ',') LIKE '%,' + #id + ',%'
See this answer.
Or consider extracting SharedBagsIds into its own table, storing comma-delimited values in a database field is not ideal.
Your table is not in 1st normal form which states that every cell should be atomic. 'SharedBagsIds' contains more than one value which should always be avoided. Read about many to many relationships in SQL and modify the table accordingly.
Suppose I have table Teacher and another table student, then to show the relationship between them, create another table which contains teacherid and studentid as the composite primary key. Thus you can show individual mapping without having the need to put multiple ids like 1,3,4 in single cell.
Hope this helps.
In my project I need to insert a table data from another table. I trying to write this sql code. But this order by option is not working while inserting the data. Here is my code:
INSERT INTO StudentInfo_MeritPosition
( ID,
Name,
MeritPosition,
SSC,
HSC,
First,
Second,
Third,
Fourth,
Fifth
)
SELECT ID,
Name,
MeritPosition,
SSC,
HSC,
First,
Second,
Third,
Fourth,
Fifth
FROM StudentInfo
ORDER BY MeritPosition
The above code inserting data into database. But not in the order format.
I need to know if there any way our for this problem. Thank you.
SQL tables represent unordered sets. When you retrieve data from the table, the data has no particular order, unless you specify order by. So, you can just retrieve the data as:
select mp.*
from StudentInfo_MeritPosition mp
order by mp.MeritPosition;
You can make this query more efficient by adding an index on StudentInfo_MeritPosition(MeritPosition).
You can use a temp table to order in any way you want. In my opinion it's easier to assemble a temp table first, then order those results and select them into the table you're trying to populate in a given order. This way you can translate it to a stored procedure and feed it the parameter of "column name" and "ASC or DESC." The temp table will take a bit longer to work with since you're selecting, ordering, reselecting, and inserting. However, the end result is much more robust than a 1 time query allowing you to use any column name, and ASC or DESC. Just remember that when you do select the results into your permanent table, you leave out the primary key (typically [P_ID]) column form your select into statement.
So, to improve on Gordon's answer, you could write something like what follows:
DECLARE #fromTbl, #sortCol, #orderByCol VARCHAR(50)
EXEC('
select mp.*
from /* StudentInfo_MeritPosition* / ' + #fromTbl + 'mp
order by /* mp.MeritPosition */ mp.' + #orderByCol + ' ' + #sortOrder;'
/* If you wanted to debug it and make sure your parameters are being
generated correctly, you can use the PRINT function instead of
Exec('Your statement above') */
Then, if you turn it into a SP you can pass in the three parameters table, order by column, and sort order (ASC|DESC) and bypass the temp table creation process I mentioned previously.
Try this.
INSERT
/*+append*/
INTO StudentInfo_MeritPosition
( ID,
Name,
MeritPosition,
SSC,
HSC,
First,
Second,
Third,
Fourth,
Fifth
)
SELECT *
FROM (
SELECT ID,
Name,
MeritPosition,
SSC,
HSC,
First,
Second,
Third,
Fourth,
Fifth
FROM StudentInfo
ORDER BY MeritPosition );
I have a data set in memory which has two data tables in it.
Cont
Ins
Secondly i have separate data table (in memory) in which i have rows in following format.
And suppose it has two rows. In actual it may have million of rows
ID TableName ColumnName Operator value
1 Const ID = 1
2 Ins app_ID = 558877
As is: Now i get information from given rows and construct a query and execute it in database server like:
Select count(*) from Cont.Id= 1 and Ins.app_id = 558877;
On the basis of above query result i have implemented my business logic.
Objective: In order to increase performance i want to execute query in application server as now i have complete table in memory. how can i do it.
Note: Tables name may vary according to data.
Do you really want to keep tables with millions of rows in memory?
In terms of counting an in memory table, how is it kept? If it's a datatable as per your tag you can use the DataTable.Rows.Count property.
If your table names aren't known, you can loop over the tables in DataSet.Tables and call Rows.Count on each of them.
You have to create index on app_ID field. After that your count will run with good performance.
Your query references undefined alias "Ins". You must specify another table in FROM/JOIN clause and change "count()" to "count(Cont.)"
i have a csv file which data is in this type...(date,time,id)
20120131, 07:17:40, BK01
20120131, 07:17:41, BK02
20120121, 07:50:04, BK05
then..i want to store this data into a table..
i read about LOAD DATA INFILE but...
as far as i know(not much), LDI
is for when the data column in my csv is totally same with my db table..
but in my db table there is another column..
which is Status..
20120131, 07:17:40, BK01, STATUSHERE
20120131, 07:17:41, BK02, ??
20120121, 07:50:04, BK05, ??
this Status based on the time in csv file...
i mean here..how can i set the status and then store it
with the data from csv file altogether into?
or is it by using data table/dataset?
thank you in advance..
LOAD DATA INFILE does not require the columns in CSV to match the columns in your table. You can specify column names in your LOAD DATA INFILE query in which you would like to insert values. Here is a text snippet from MySQL documentation
The following example loads all columns of the persondata table:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;
By default, when no column list is provided at the end of the LOAD DATA INFILE
statement, input lines are expected to contain a field for each table
column. If you want to load only some of a table's columns, specify a
column list:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata
(col1,col2,...);
You must also specify a column list if the order of
the fields in the input file differs from the order of the columns in
the table. Otherwise, MySQL cannot tell how to match input fields with
table columns.
However, I'm not sure I fully understand your need -
NEED 1: would you like to insert only the 3 columns in your CSV file to your table ignoring what goes into the Status column? If yes, the above can help.
NEED 2: would you like to insert values into Status column? In that case, simply add a fourth column to your CSV file and then run LOAD DATA INFILE.
NEED 3: would you like to calculate the values for Status column at run-time? Use SET syntax in this case. The below text snippet might help:
The column list can contain either column names or user variables.
With user variables, the SET clause enables you to perform
transformations on their values before assigning the result to
columns.
User variables in the SET clause can be used in several ways. The
following example uses the first input column directly for the value
of t1.column1, and assigns the second input column to a user variable
that is subjected to a division operation before being used for the
value of t1.column2:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, #var1) SET
column2 = #var1/100;
The SET clause can be used to supply values not derived from the input file. The following statement sets column3 to the current date and time:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, column2) SET
column3 = CURRENT_TIMESTAMP;
You can also discard an input value by
assigning it to a user variable and not assigning the variable to a
table column:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, #dummy,
column2, #dummy, column3);
Hope it helps! You may read more on the documentation page.
I have a problem with this:
String or binary data would be truncated
And this is my coding:
String savePath = #"C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile\" + fileName;
inputImageBox.Image = Image.FromFile(ImageLocation);
inputImageBox.Image.Save(savePath);
String sqlData = "INSERT INTO CharacterOCR(ImageName, ImagePath, Character, CharacterDescription)
VALUES('"+fileName+"', '"+savePath+"', '"+typeName+"', '"+CharDesc+"')";
SqlCommand cmd = new SqlCommand(sqlData, con);
cmd.ExecuteNonQuery(); // <<<<<error indicates here
What is the problem?
Look at the schema of your table and look at the max lengths. You're probably trying to insert something larger than the max length.
That just means that one of the values you're trying to cram into a column is too long for the data type you've defined for that column.
For example, trying to INSERT "Hello" to a column that's only a VARCHAR(4).
It's generally considered better coding practice to store the paths to where your images are in your database, while keeping your images stashed in the appropriate directory structure.
If you're set on inserting image data into a database, kindly let everyone know what version your database is.
EDIT:
Pull up your SQL Schema for the ImagePath column, and tell me how many characters you have it defined to accept.
Next, before you execute your SQL, call Console.WriteLine(savePath.Length).
Is savePath.Length greater than your ImagePath column definition?
You will get this error, when the column length is different when compared with the table definition and storedprocedure parameter.
Example:
I have a table called
Employee
with a column Reason
and while declaring the table the column length for Reasonis varchar(100)
and while writing a stored procedure for the Employee table i am declaring Reason parameter as #Reason varchar(250).
In this situation you will be getting this error.
So make sure that the column length is unique.