Are there any commands that make life easy with respect to this? I want to take the column schema of one datatable (.net datatable) and copy it to another new datatable.
I've seen something like:
SELECT * INTO [DestinationTable] FROM [SourceTable] WHERE (1=2);
Used in SqlServer.
I think this assumes that DestinationTable doesn't exist. It then creates the table and copies the schema from SourceTable the WHERE clause prevents any actual data from being copied.
I'm not really a database developer, so there's probably a much better way to do this.
I've found the answer. It is the .Clone method.
Related
So I recently came across this solution in code and was wondering if it is considered an 'acceptable' way of retrieving a single row from a Strongly Typed dataset/table.
Basically what we have is something like this:
Dim fooAdapter As New FooDataSetTableAdapters.FooTableAdapter()
Dim fooRow As FooDataSet.FooRow = CType(fooAdapter.GetData().Select("SomeFooField=50")(0), FooDataSet.FooRow)
with fooRow
..
..
end with
Although the code above works my main concern is that calling fooAdapter.GetData() will actually grab all the data in the table before applying the Select() filter which could potentially slow things down over time... Is there a cleaner way to do this or is this way of doing things fine?
Edit:
The filter criteria is not the primarykey field so calling the table's FindByFooID method will not work...
Use the dataset editor tool to create a query GetUniqueFoo with the SQL that will filter based on SomeFooField. You can run it by using fooAdapter.GetUniqueFoo(SomeFooValue).
Is there a way to query the creation date of a table in SQLite?
I am new to SQL, overall. I just found this SQL Server table creation date query.
I am assuming that sqlite_master is the equivalent to sys.tables in SQLite. Is that correct?
But then my sqlite_master table only has the columns "type", "name", "tbl_name", "rootpage" and "sql".
If this is not possible in SQLite, what would be the best way to implement this functionality by myself?
SQLite does not store this data itself. Like you said, the sqlite_master table doesn't have any relevant column.
There's no particularly nice way that I can come up with to implement it. You could create some sort of interface for creating tables, and have it note the time whenever you create a new one, but anything created through a different method won't go through the same process. It also looks like there's no way to set a trigger on CREATE TABLE, so that's not an option either.
Why do you want this functionality? Creating tables seems like something you wouldn't be doing very often, maybe there's a better way to approach the problem?
I need to modify a column in a SQLite database but I have to do it programatically due to the database already being in production. From my research I have found that in order to do this I must do the following.
Create a new table with new schema
Copy data from old table to new table
Drop old table
Rename new table to old tables name
That seems like a ridiculous amount of work for something that should be relatively easy. Is there not an easier way? All I need to do is change a constraint on a existing column and give it a default value.
That's one of the better-known drawbacks of SQLite (no MODIFY COLUMN support on ALTER TABLE), but it's on the list of SQL features that SQLite does not implement.
edit: Removed bit that mentioned it may being supported in a future release as the page was updated to indicate that is no longer the case
If the modification is not too big (e.g. change the length of a varchar), you can dump the db, manually edit the database definition and import it back again:
echo '.dump' | sqlite3 test.db > test.dump
then open the file with a text editor, search for the definition you want to modify and then:
cat test.dump | sqlite3 new-test.db
As said here, these kind of features are not implemented by SQLite.
As a side note, you could make your two first steps with a create table with select:
CREATE TABLE tmp_table AS SELECT id, name FROM src_table
When I ran "CREATE TABLE tmp_table AS SELECT id, name FROM src_table", I lost all the column type formatting (e.g., time field turned into a integer field
As initially stated seems like it should be easier, but here is what I did to fix. I had this problem b/c I wanted to change the Not Null field in a column and Sqlite doesnt really help there.
Using the 'SQLite Manager' Firefox addon browser (use what you like). I created the new table by copying the old create statement, made my modification, and executed it. Then to get the data copied over, I just highlighted the rows, R-click 'Copy Row(s) as SQL', replaced "someTable" with my table name, and executed the SQL.
Various good answers already given to this question, but I also suggest taking a look at the sqlite.org page on ALTER TABLE which covers this issue in some detail: What (few) changes are possible to columns (RENAME|ADD|DROP) but also detailed workarounds for other operations in the section Making Other Kinds Of Table Schema Changes and background info in Why ALTER TABLE is such a problem for SQLite. In particular the workarounds point out some pitfalls when working with more complex tables and explain how to make changes safely.
I will explain problem with an example:
There is two table in my database, named entry, tags
There is a column named ID_ENTRY in both table. When I add a record to table, entry, I have to take the ID_ENTRY of last added record and add it to table, tags. How can I do it?
The only way to do this is with multiple statements. Using dynamic sql you can do this by separating each statement in your query string with a semi-colon:
"DECLARE #ID int;INSERT INTO [Entry] (...) VALUES ...; SELECT #ID = scope_identity();INSERT INTO [TAGS] (ID_ENTRY) VALUES (#ID);"
Make sure you put this in a transaction to protect against concurrency problems and keep it all atomic. You could also break that up into two separate queries to return the new ID value in the middle if you want; just make sure both queries are in the same transaction.
Also: you are using parameterized queries with your dynamic sql, right? If you're not, I'll personally come over there and smack you 10,000 times with a wet noodle until you repent of your insecure ways.
Immediatly after executing the insert statement on first table, you should query ##IDENTITY doing "SELECT ##identity". That will retrieve the last autogenerated ID... and then just insert it on the second table.
If you are using triggers or something that inserts rows... this may be not work. Use Scope_Identity() instead of ##IDENTITY
I would probably do this with an INSERT trigger on the named entry table, if you have all of the data you need to push to the tags table available. If not, then you might want to consider using a stored procedure that creates both inside a transaction.
If you want to do it in code, you'll need to be more specific about how you are managing your data. Are you using DataAdapter, DataTables, LINQ, NHibernate, ...? Essentially, you need to wrap both inserts inside a transaction of some sort so that either inserts get executed or neither do, but the means to doing that depend on what technology you are using to interact with the database.
If you use dynamic sql, why not use Linq to Entity Framework, now EF is the recommend data access technology from Microsoft (see this post Clarifying the message on L2S Futures from ADO.NET team blog), and if you do an insert with EF the last identity id will available for you automatically, I use it all the time it's easy.
Hope this helps!
Ray.
Using VB.NET, how do I add the values from an XML file into an SQL Server database with a similar schema?
I don't know the methods off the top of my head but you ought to be able to load the file into a DataTable and then use ADO.net to get it into the database (look into the SqlBulkCopy class if you're using a SQL Server DB).
This ought to get you enough words to punch into and find a good example.
Easiest way is using the DataSet class's ReadXml() method. Then, as the user above mentions, the dataset contains one (1) DataTable. It's pretty easy to get that into SQL.
Use an XSL Transform to create an insert statement.
can't use xsl transform, you need a string for query, not another xml