Best way to duplicate data in the same table and updating a column at the same time - sql

I'm trying to find the best way to duplicate all the rows in a table.By this i mean inserting them again in the same table, but i need to update a single column only on the values that were inserted.
This is to help me write a script so I can automate some work on some clients.
I can't use select * as it will throw an error because of the identity columns but at the same time i don't want to be manually writting all the column names for several tables.
Is there a simple way to translate this into SQL server?
Sorry for not showing a piece of code, but i have nothing at the moment and i'm not really fluent in SQL.
EDIT: I have ended up following the advice of JamieD77 in the comments below this post by moving everything to a table, drop the id column, updating what i need and then moving back as it seems to be the most effiecient.

In SQL Server Management Studio you can drag the "Columns" folder under a table and drop it on the query window and it will paste a comma-delimited list of all the columns.
Or run a query like:
select string_agg(quotename(name),', ')
from sys.columns
where object_id = object_id('MyTable')

Related

SQLite: How to add a column to all the tables in the database?

I am quite new to SQLite (SQL overall). I created a database and created multiple tables in it and suddenly realized I had forgot to add a column to them. Now I have 280 tables having distinct names without a column. I have tried adding * and combining with SELECT but it gives an error. How can I accomplish it?
my code:
ALTER TABLE "MYBUS.*" ADD COLUMN STOPID int null;
After 3 hours issue finally solved.
After searching for sometime, I stumbled upon this answer
SQlite alter table with result from select statement
I ran the code:select 'alter table '||tbl_name||' add STOPID int null' from sqlite_master
It returned all the 280 SQL queries needed but with quotation marks, so I took the clue from this.lau and created a python regex script and ran it. Worked like a charm and within 10 lines of code I was able to add a column to all my 280 tables.
See this link https://dba.stackexchange.com/questions/160145/add-columns-to-all-tables-in-a-database-if-the-columns-dont-exist
Even though it applies to SQL Server, this should help you in getting started.
Ensure that you perform a check on the column name before adding it to a table.

MS Access SQL (Quickbooks) Update Query

I'm trying to create an Update Query in MS Access (2013) to a QuickBooks Database using QODBC.
I need to update the table PriceLevelPerItem. I am trying to update the field in said table called PriceLevelPerItemCustomprice with a value from another table, QueryThreeTable, and a column titled UpdatedPrice.
I need to update the table PriceLevelPerItem where the PriceLevelPerItemItemRefListID matches the value of ItemID from QueryThreeTable and ListID matches the QueryThreeTable.ItemListID (yes I know these are the wrong way around...)
So far this process has been a very annoying trial of many queries and any help would be greatly appreciated
This is what I've been working with:
UPDATE
PriceLevelPerItem
SET
(PriceLevelPerItemCustomPrice = QueryThreeTable.UpdatedPrice)
FROM
QueryThreeTable, PriceLevelPerItem
WHERE
QueryThreeTable.ItemID = PriceLevelPerItem.PriceLevelPerItemItemRefListID
AND
QueryThreeTable.ItemListID = PriceLevelPerItem.ListID;
I think the problem is that you're trying to use a DAO query inside a QODBC query. I think the two use different Data Access engines.
You're going to need to lookup your UpdatedPrice in your QueryThreeTable using DLookup. Or maybe you need to create a DAO loop using QueryThreeTable that then updates values in your QODBC table from there.
Make your QODBC query work without the use of QueryThreeTable and without any joins. Then come up with a way to dynamically create your query. You're resulting SQL should look something like this:
UPDATE
PriceLevelPerItem
SET
PriceLevelPerItemCustomPrice = 150.16
WHERE
PriceLevelPerItem.ListID = '310000-1146238368';

Create delimited string from a row in stored procedure with unknown number of elements

Using SQL Server 2000 and Microsoft SQL Server MS is there a way to create a delimited string based upon an unknown number of columns per row?
I'm pulling one row at a time from different tables and am going to store them in a column in another table.
A simple SQL query can't do anything like that. You need to specify the fields you are concatenating.
The only method that I'm aware of is to dynamincally build a query for each table.
I don't recall the structure of MSSQL2000, so I won't try to give an exact example, maybe someone else can. But there -are- system tables that contain table defintions. By parsing the contents of those system tables you can dynamically build the necessary query for each source data table.
TSQLthat writes TSQL, however, can be a bit tricky to debug and maintain :) So be careful how you structure everything...
Dems.
EDIT:
Or just do it in your client application.

Combining data from Excel with database

This is probably a simple question, but I really don't know what I'm doing in Excel, so hopefully someone can help me out.
I've been given an Excel spreadsheet that has two relevant columns to my task. The first column is an "External ID", and the second column is an "Internal ID". I need to select a bunch of data out of our databases (with various joins) using the Internal ID as the key, but then all of this data needs to be linked back to the External ID, and the only link between Internal/External is this spreadsheet.
For example, say a row of the spreadsheet looks like this:
ExtID IntID
AB1234 2
I need to select all the data relevant to the item with ID #2 in our database, but I have no way to get "AB1234" from the database, so I need to somehow relate this data back to "AB1234" using the spreadsheet.
What's the easiest way to accomplish this? The version of Excel is Excel 2007, and the database is Oracle, if that's relevant.
Note that I only have read permission to the production databases, so creating tables and importing the spreadsheet data to do a join is not an option.
Edited based on a comment
1 - Use MS Access to import the Excel sheet as a table.
2 - Link to your database table, also from within MS Access
External Data tab->other data sources->ODBC connection->choose yours->pick the table(s) you want
3 - Write an Access query to compare the values you want
Create->Query Design->Drop the tables you want, drag lines between them for relationships, click Run
Usually I use copy-paste and a good column-mode editor with macros to accomplish such tasks. It works fine if you only have a couple of Excel files.
Alot depends on how familiar you are with the tools you have available to you.
DO you have a tool you are familiar with that would make it easy to use the IntID to find those records? If so, can you do the query and paste the results back into the original spreadsheet in the column to the right of the column with the IntID?
If so, you will have what you want, a spreadsheet with the following columns:
ExtID (original)
IntID (original)
IntID (from Oracle)
Col1 (from Oracle)
Col2 (from Oracle) etc....
I'm not familiar with Oracle, but I know a lot of databases let you prepend a table name with # or something like that and create a temp table. Others have a temporary database where you can create things. Sometimes you can create a temp table even if you can't do anything else but select.
If you have access to do that, I would do the function as JosephStyons suggests (#2), insert your records into the temp table, and do a query based on that.
With Excel and VBA, you can use ActiveX Data Objects (ADO) as a high level way of using the OLE DB provider for a particular database. This lets you read the data from the database and you can then query that data and store the results in the spreadsheet.
Oracle OLE DB provider
ADO Guide

Queries in MS-Access:formatting a field in the middle of an sql UPDATE code

I am having yet another problem with my data in ms-access.
Basically, what i'm doing is using multiple sql statements to pull, sort, and staight up change/manipulate data. The problem that im having currently is that i am trying to use a sql code like this:
UPDATE analyzedCopy2 SET analyzedCopy2.DateRange = #4/21/2009# to #4/29/2009#
but i can't figure out how to reformat DateRange (which is currently a date) to Text in the sql statement. I can't simply set the format manually because the sql code before this one is pulling in all of the data from another table and when that happens it changes the format of DateRange to date automattically since that is the type it is where it is coming from.
If anyone knows how to format a column with vba code then that would be fine too since i'm creating a vba code to run the queries in the order i want.
If anyone needs a better explanation of anything just let me know.
You can use SQL (DDL) to change some aspects of a table:
ALTER TABLE TableName ALTER COLUMN ColumnName Text(50)