I am working with the code below but it is returning an error -
Incorrect syntax near the keyword 'AS'
I expect the query to create a table using the data from existing tables.
create table Allactivity as
(
select dbo.dim_client.*,dbo.dim_transaction.*
from dbo.dim_client,dbo.dim_transaction,dbo.fact_capital
where dbo.dim_client.dim_client_key=dbo.fact_capital.dim_client_key
and dbo.dim_transaction.dim_transaction_key=dbo.fact_capital.dim_transaction_key
)
It looks like you're using Microsoft SQL Server. T-SQL has a different syntax: SELECT INTO:
http://msdn.microsoft.com/en-us/library/ms190750.aspx
select dbo.dim_client.*,dbo.dim_transaction.*
into Allactivity
from dbo.dim_client,dbo.dim_transaction,dbo.fact_capital
where dbo.dim_client.dim_client_key=dbo.fact_capital.dim_client_key
and dbo.dim_transaction.dim_transaction_key=dbo.fact_capital.dim_transaction_key
If you use the select into syntax, you will need to have rights to dynamically create a table.
Another solution is to work with a DBA to create the staging table ahead of time. Then clear and load the staging table each time. Have the DBA give you or the application rights to the table. This can be db_datareader & db_datawriter which is a-lot less access than db_owner!
I did notice that you are using older syntax for joins, you should use the newer syntax for forward compatibility.
http://www.straightpathsql.com/archives/2012/11/sql-server-join-syntax-its/
--
-- DBA - Create empty new table (assumes stage schema exists)
--
select
dc.*,
dt.*
into
stage.all_activity
from
dbo.fact_capital as fc
left join
dbo.dim_client as dc
on fc.dim_client_key = dc.dim_client_key
left join
dbo.dim_transaction as dt
on fc.dim_transaction_key = dt.dim_transaction_key
where (1 = 0);
-- User/Application - clear the table
delete from stage.all_activity;
-- User/Application - add some data (+ where clause for range)
insert into
stage.all_activity
select
dc.*,
dt.*
from
dbo.fact_capital as fc
left join
dbo.dim_client as dc
on fc.dim_client_key = dc.dim_client_key
left join
dbo.dim_transaction as dt
on fc.dim_transaction_key = dt.dim_transaction_key;
Related
I am working with AS400 version 7.1.
Having the following:
ServerA (SA) - DatabaseA (DBA) - TableA (TA)
ServerB (SB) - DataBaseB (DBB) - TableB (TB)
SELECT A.*, B.*
FROM SA.DBA.TA A INNER JOIN SB.DBB.TB
ON A.PN=B.PN
WHERE A.PN='BFDKS';
What's the correct syntax to join 2 tables from two different servers in AS400?
I am getting the following error
Relational database SA not in relational database directory
I'm pretty sure this is not currently possible with Db2 for i...
3-part names are new to the i, and as far as I know are limited to
insert into mylib.mytable
(select * from remotedb.somelib.sometable);
see CREATE TABLE with remote subselect
Or in a trigger program..
see 3-part names in triggers
Db2 for LUW has such federation capabilities...
One work around I've seen is the use of a user defined table function (UDTF) to return rows from a remote Db2 for i database..
Not possible on DB2 for i. But like says Charles, you can do it :
-- run this instructions on server B
create table qtemp.SADBATA as (
SELECT A.* FROM SA.DBA.TA A
where A.PN='BFDKS'
) with data;
SELECT * FROM qtemp.SADBATA A INNER JOIN SB.DBB.TB ON A.PN=B.PN;
So at work we've been using Oracle SQL Developer 4.1.3.20 lately to query our data so I wrote this snippet of code (its actually much longer by 8 or so more table joins):
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from xyz.DRUG_KIT k
left join xyz.DR_SHIP s on s.ID = k.SHIPMENT_ID
left join xyz.USR_PAT u on u.PAT_ID = k.PAT_ID
When I have to switch to another table in a different database, I have been copy pasting everywhere above where you see 'xyz' but surely there has to ba better way? I use to use SQL Server Management Studio 2014 but we had to switch to this once we began using oracle databases. How would I declare a varchar(?) variable in Oracle SQL Developer so I can just use that one variable in place of XYZ and at the top of the query I just can set that variable equal whatever necessary in one place if that makes sense. It becomes cumbersome when I copy paste something wrong on one line when I"m trying to join about 10+ tables together to retrieve data.
XYZ is the owner of the table, not a different database. If you are connected as user XYZ, then you don't need to prefix XYZ to the table name, it's only required if you are attempting to access objects owned by a different "SCHEMA".
You can change the current "parsing" schema of your session by issuing an alter session command:
ALTER SESSION SET CURRENT_SCHEMA=PDQ;
and then select from objects the user PDQ has granted you select privs on:
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from DRUG_KIT k
left join DR_SHIP s on s.ID = k.SHIPMENT_ID
left join USR_PAT u on u.PAT_ID = k.PAT_ID;
You can then alter your schema back to XYZ and perform the same select statement and get different results based on the contents of the tables as owned by XYZ:
ALTER SESSION SET CURRENT_SCHEMA=XYZ;
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from DRUG_KIT k
left join DR_SHIP s on s.ID = k.SHIPMENT_ID
left join USR_PAT u on u.PAT_ID = k.PAT_ID;
To make the DR_SHIP table owned by PDQ accessable from the XYZ schema, a private synonym to PDQ.DR_SIP can be created in place of the table onwed by XYZ:
CREATE OR REPLACE SYNONYM DR_SHIP FOR PDQ.DR_SHIP;
On the other hand if you want the PDQ version of the DRUG_KIT table accessible from all SCHEMAs in the DB, you can create a public synonym:
CREATE OR REPLACE PUBLIC SYNONYM DRUG_KIT FOR PDQ.DRUG_KIT;
If none of the above meet your needs, you can use SQL*Plus style substitution variables:
ACCEPT user;
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from &user..DRUG_KIT k
left join &user..DR_SHIP s on s.ID = k.SHIPMENT_ID
left join &user..USR_PAT u on u.PAT_ID = k.PAT_ID;
But you need to understand that these won't necessarily work outside of SQL*Plus, SQL Developer, SQLcl, and possibly a couple of other SQL development environments.
At the database level, you probably want public synonyms. https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7001.htm
Struggling with a create table statement which is based on this select into statement below:
#MaxAPDRefundAmount money = 13.00
...
select pkd.*, pd.ProviderReference, per.FirstName, per.Surname, #MaxAPDRefundAmount [MaxAPDRefundAmount],commission.Type [Commission] into #StagedData from CTE_PackageData pkd
inner join J2H.dbo.Package pk on pkd.Reference = pk.Reference
inner join J2H.dbo.Product pd on pk.PackageId = pd.PackageId
inner join J2H.dbo.FlightReservation fr on pd.ProductId = fr.ProductId
and fr.FlightBoundID = 1
inner join J2H.dbo.ProductPerson pp on pd.ProductId = pp.ProductID
and pp.StatusId < 7
inner join J2H.dbo.Flight f on fr.FlightId = f.FlightID
inner join J2H.dbo.Person per on pk.PackageId = per.PackageId
and per.PersonId = pp.PersonId
inner join J2H.dbo.PersonType pt on per.PersonTypeId = pt.PersonTypeID
We are changing a select into to just normal insert and select, so need a create table (we are going to create a temp (hash tag table) and not declaring a variable table. Also there is a pkd.* at the start as well so I am confused in knowing which fields to include in the create table. Do I include all the fields in the select statement into the create statement?
Update:
So virtually I know I need to include the data types below but I can just do:
create table #StagedData
(
pkd.*,
pd.ProviderReference,
per.FirstName,
per.Surname,
#MaxAPDRefundAmount [MaxAPDRefundAmount],
commission
)
"Do I include all the fields in the select statement into the create statement?" Well, that depends, if you need them, than yes, if not than no. It's impossible for us to say whether you need them... If you're running this exact query as insert, than yes.
As for the create statement, you can run the query you have, but replace into #StagedData with something like into TEMP_StagedData. In management studio you can let sql server build the create query for you: right-click the newly created TEMP_StagedData table in the object explorer (remember to refresh), script Table as, CREATE To and select New Query Editor Window.
The documentation of the CREATE TABLE statement is pretty straightforward.
No. Clearly, you cannot use pkd.* in a create table statement.
What you can do is run your old SELECT INTO statement as a straight SELECT (remove the INTO #stagedata) part, and look at the columns that get returned by the SELECT.
Then write a CREATE TABLE statement that includes those columns.
To create a table from a SELECT without inserting data, add a WHERE clause that never returns True.
Like this:
SELECT * INTO #TempTable FROM Table WHERE 1=0
Once the table with the columns for your SELECT, you can add additional columns with ALTER TABLE.
ALTER TABLE #TempTable ALL ExtraColumn INT
Then do your INSERT/SELECT.
Please consider the following:
use db1;
select * into #db1_tmp from mytable;
use db2;
select * into #db2_tmp from myothertable;
-- join temp table 1 and 2
select * from #db1_tmp a
left join db2_tmp b on where a.uid = b.uid;
This works, but SQL Server Management Studio is red-underlining #db1_tmp in the last query and therefore in all other statements that depend on this table.
Question: what is the proper way to access a temp table created in another database to prevent this underlining from happening? I tried db1.#db1_tmp but this does not work. I'm on SQL Server 2008.
Temp tables actually appear in their own database, TempDB. I think the root of your issue is the use statements. Try this instead:
select * into #db1_tmp from db1.dbo.mytable;
select * into #db2_tmp from db2.dbo.myothertable;
-- join temp table 1 and 2
select * from #db1_tmp a
left join db2_tmp b on where a.uid = b.uid;
But if this is the extent of what you're doing (creating the temp tables just to do a join across the databases), you can skip the temp tables altogether:
select * from db1.dbo.mytable a join db2.dbo.myothertable b on a.uid = b.uid.
Temp tables has no use of Database reference for creation so gets created in tempdb.
Only the source can be changed with "use" or "dbname.dbo.mytable".
The red-underlining is due to intellisense. The temp table is just identified as a normal table before execution and redlined due to database change.
Note: The select query at the last has syntax errors.
it should be,
select * from #db1_tmp a left join #db2_tmp b on a.uid = b.uid;
Hi i got a little problem right now i'm doing Stored Proc in sql server. I wanna get some result from multiple table and put it into a temporary table. So I thought i could use a "Select into" statement wich work fine until i decided to add multiple select into the temporary table. For example: Here's my code:
while #Compteur <= #wrhCodeEnd
select lp.lotQty ,lp.lotID into ZeTable from TB_lotLot ltlt
inner join TB_lotPal lp on ltlt.lotID=lp.lotID
inner join TB_palSuper ps ON lp.spID=ps.spID
inner join TB_expExpeditionLot eel ON eel.lotID=lp.spID
where ps.wrhID <> #Compteur and
ltlt.wrhID = #Compteur and
lp.lotID not in(select ZeTable.lotID from ZeTable)
the thing is I don't know if I can make multiple select into on the same temporary table and I also wanna check with a where clause the information in the table is not already there.
Thx in Advance
You can create temporary table and can use insert statement to add records with required columns and can drop it after use.