Copy date from table to another table in a different db - sql

Cant work out what I'm doing wrong here, I'm trying to copy the data from table to another in a different db. My Sql is
SELECT * INTO [dbo].[dbname].table FROM thistable
Should be really simple but it won't work.

It should be
SELECT * INTO [dbname].[dbo].table FROM thistable
Cheers!

You should use fully qualified name on both database names then it will work.
SELECT *
INTO <databasename>.<schema>.<table>
FROM <databasename>.<schema>.<table>;

Related

How to find a specific table from my database with an sql script

i'm a total beginner with sql / data base but i'm trying to learn on a fake data base.
My problem is that i want to find a specific Table from a Schemas and i only know that in the table name there's "CRH".
I've been searching on every tutorial but none of them seems to be working i'm probably doing something wrong like i don't have the right dependencies.I'm using data beaver and i didn't modified it after the download.
here's my code :
SELECT * FROM SCHEMAS_NAME WHERE name LIKE '%CRH%'
You are using the Oracle 12c DBMS so try this:
SELECT table_name FROM all_tables
WHERE table_name LIKE '%CRH%';
Assuming your database is MS SQL Server: Try following code.
select * from sys.all_objects where type = 'U' and name like '%CRH%'

SQL using REPLACE on one of many columns

I have the following query:
SELECT * FROM MailingList
There are about 20+ columns in the MailingList table, one which is called Address. This column has some fields which contain commas, which I need to take out. So I updated my query to:
SELECT REPLACE(Address, ',', '') AS Address, * FROM MailingList
But now I have two Address columns. Is there a way to only display one Address column while still using the wildcard (*) for all the other columns?
There is not a way to do this, though listing the columns you want explicitly is a good idea anyway.
You can trick as following query:
Get the data into a temp table
Drop the cloumns that are not needed
Get results and drop temp table
SELECT *, REPLACE(Address, ',', '') AS Address2
INTO #TempTable
FROM MailingList
ALTER TABLE #TempTable
DROP COLUMN [Address]
SELECT * FROM #TempTable
DROP TABLE #TempTable
I agree with Shadow - avoid using the * wild card if you can...
I know listing out ALL of the columns in select statement for big tables is a pain so here is a quick short cut you may not be aware of: In SQL Server Management Studio, browse through the object explorer and find the table you want to select from (MailingList). Right-click it to view the context menu and choose "Script Table as" then "SELECT TO" then "New Query Editor Window". This will create a new select statement with each column spelled out. In the future, use this method to create select statements, queries, procedures, etc. rather then the * wildcard. Performance is better and it just looks nicer :-)
Then you can solve your alias issue with the replace function.

Unable to select data locally

Right, so when I run:
Select * from Consultants
I get:
ORA-00942: table or view does not exist
The table clearly exists in the object browser.
However, when I run:
select * from Customers#xe.m512Finn
I get the data requested.
Please don't tell me that the table in the local database doesn't exist because it does; I created it and it's there in the object browser.
Is there something wrong with my syntax? Please help me with any suggestions.
Here you're selecting from the Consultants table
Select * from Consultants
And here you're selecting from the Customers table
select * from Customers#xe.m512Finn
The table does exists in m512Finn user that's why it returns the values for
select * from Customers#xe.m512Finn
You might be running that query in a different schema or the query u might be looking for must be
Select * from consultants#xe.m512Finn
Okay i've resolved the issue, it's that when you query local tables, you need double quotation marks around the table name.
select * from "Consultants"

SQL LIKE query not working

I'm trying to run the following query against an Oracle DB, but the query is returning 0 records:
select * from TABLE
where upper(FIELD) like '%SEE COMMENT%'
I know this field contains many records with 'See Comment" in it. For example, here is one of the records:
=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))
I am guessing that the quotation marks in the field are messing the query up, but im not sure how to get around it. Any suggestions?
This works for me:
create table testLike (aCol varchar2(500) );
INSERT INTO TESTLIKE VALUES('abc');
insert into testLike values('=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))');
SELECT *
FROM TESTLIKE TL
WHERE upper(tl.acol) like '%SEE COMMENT%';
can you recreate?
edit:
in your query try this:
select * from TABLE
WHERE UPPER(FIELD) = '=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))';
see if that comes up with any results
Just realized there were two similarly named fields in this table, and I was choosing the wrong one.

How to Unselect The Field in select Query using sql

hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.