How to replace string from a snowflake table - snowflake-cloud-data-platform

I have a snowflake table as TEST_TABLE
SOURCE_RECORD_ID COL_A COL_B COL_C
paragon_altdata:model:2021-04-29_003108:1 A A A
paragon_altdata:model:2021-04-29_003108:2 A A A
paragon_altdata:model:2021-04-29_003108:3 A A A
paragon_altdata:model:2021-04-29_003108:4 A A A
paragon_altdata:model:2021-04-29_003108:5 A A A
paragon_altdata:model:2021-04-29_003108:6 A A A
paragon_altdata:model:2021-04-29_003108:7 A A A
paragon_altdata:model:2021-04-29_003108:8 A A A
paragon_altdata:model:2021-04-29_003108:9 A A A
In this table i want to replace partial string paragon_altdata to paragonintel_altdata from the first column
My table should look like below
SOURCE_RECORD_ID COL_A COL_B COL_C
paragonintel_altdata:model:2021-04-29_003108:1 A A A
paragonintel_altdata:model:2021-04-29_003108:2 A A A
paragonintel_altdata:model:2021-04-29_003108:3 A A A
paragonintel_altdata:model:2021-04-29_003108:4 A A A
paragonintel_altdata:model:2021-04-29_003108:5 A A A
paragonintel_altdata:model:2021-04-29_003108:6 A A A
paragonintel_altdata:model:2021-04-29_003108:7 A A A
paragonintel_altdata:model:2021-04-29_003108:8 A A A
paragonintel_altdata:model:2021-04-29_003108:9 A A A
Please anyone help me on this

You can just use replace and then to create a new table do a create table as select
create NEW_TABLE as
select
replace(SOURCE_RECORD_ID, 'paragon_', 'paragonintel_') as SOURCE_RECORD_ID
,COL_A
,COL_B
,COL_C
from TEST_TABLE
;
You can then swap them and drop the old one.

Related

Create new tables from old tables [duplicate]

This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'
Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table
use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]
Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)
Please try:
SELECT * INTO NewTable FROM OldTable
Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] into [New Table] from [Source Table]

SQL trigger with IDENTITY_INSERT

I have two tables: Table1 is all the companies, Table2 is companies whose name start with A.
Table1 company (companyId int, companyName varchar(50), companySize int)
Table2 companyStartWithA (companyId int, companyName varchar(50), companySize int)
What I want to do is to create a trigger so that when I insert/update/delete something in Table1, it will automatically do the same in Table2
My code:
CREATE TRIGGER A_TRG_InsertSyncEmp
ON company
AFTER INSERT
AS
BEGIN
INSERT INTO companyStartWithA
SELECT *
FROM INSERTED
WHERE inserted.companyName LIKE 'A%'
END
And I get an error:
An explicit value for the identity column in table 'companyStartWithA' can only be specified when a column list is used and IDENTITY_INSERT is ON.
What can I do?
Thanks
The problem is the fact that you're not explicitly specifying the column in the INSERT statement, and using a SELECT * to fill the data. Both are big no-no's - you should always explicitly specify the column that you want to insert into, and you should always explicitly specify the columns that you want to select. Doing so will fix this problem:
CREATE TRIGGER A_TRG_InsertSyncEmp
ON company
AFTER INSERT
AS
BEGIN
INSERT INTO companyStartWithA (companyName, companySize)
SELECT companyName, companySize
FROM INSERTED
WHERE inserted.companyName LIKE 'A%'
END
But as Sean Lange absolutely correctly commented - this should really be just a view rather than a separate table.....
CREATE VIEW dbo.CompanyStartsWithA
AS
SELECT companyId, companyName, companySize
FROM dbo.Company
WHERE Name LIKE 'A%'
and then you don't need any messy triggers or anything - just insert into dbo.Company and all companies with a name that starts with an A will be visible in this view....

Inserting data from two temp tables into single table

I have payment table and Claim table and TmpProcessClaims table with the following columns in each table \
Claim : ClaimID pk, paymentID (FK),EPPID,Claimnumber,GroupNumber, certificate number
Payment : paymentID (pk),EPPID, checkDate
TmpProcessClaims: TmpProcessClaimsID(pk),EPPID, ClaimNumber,Administrator,GroupNumber,
here is what i need to do... I need to take the EPPID from TmpProcessClaims and search the same EPPID in payments table and if results are there in payment table i need to insert the results into claim table from both tables payments and TmpProcessClaims
CREATE PROCEDURE [dbo].[InsertClaims]
AS
BEGIN
CREATE TABLE #TEMPEPPID ([EPPID] VARCHAR(150), [PaymentID] BIGINT)
CREATE TABLE #TEMPCLAIM ([EPPID] VARCHAR(150), [GroupNumber] varchar(10),[ClaimNumber] varchar(50), [CertificateNumber] varchar(15))
SELECT EPPID , PaymentID
INTO #TEMPEPPID
FROM DBO.Payment
SELECT EPPID, [GroupNumber],[ClaimNumber],[CertificateNumber]
INTO #TEMPCLAIM
FROM [dbo].[TmpProcessClaimsToMedPay]
where EPPID in (select EPPID from #TEMPEPPID)
INSERT INTO Claim ....
END
GO
But i am not sure how to insert the data from two temp table into single table
is this is correct way to proceed or any other ways to go through this criteria
could any one pls help on this issue ..
many thanks in advance ...
Just join the tables and insert:
INSERT INTO Claim([column names])
SELECT [column names]
FROM DBO.Payment AS p
INNER JOIN [dbo].[TmpProcessClaimsToMedPay] AS c
where p.EPPID = c.EPPID

Insert into a table several values in a temp table

I have several values in a temp table called #tempIQ and I want to insert into a table called IQGroups using the same Group identifier. Assuming everyone has a unique IQ:
create table #tempIQ
(
id int
)
declare #GroupIDas int
set #GroupID=1001
select iq from #tempIQ
1,2,86,99,101,165,180,201
I want to insert these ids from the temp table into a grouping called IQGroups but am having difficulty finding a simple solution.
-- now try and insert all the iqs for a group into the IQGroups table from the #tempIQ table.
insert into IQGroups (GroupID, IQ) values (#GroupID, #tempiQ.iq)
Try this:
INSERT INTO IQGroups (GroupID, IQ)
SELECT #GroupID, IQ
FROM #tempIQ
Try using the SELECT statement.
INSERT INTO IQGroups (GroupID, IQ)
SELECT #GroupID, iq
FROM #tempIQ
This is the standard way to select multiple rows.
this is another way to do this,
select id, 1001 as GroupID
into IQGroups
from #tempIQ

How to save select query results within temporary table?

I need to save select query output into temporary table. Then I need to make another select query against this temporary table. Does anybody know how to do it?
I need to make this on SQL Server.
select *
into #TempTable
from SomeTale
select *
from #TempTable
You can also do the following:
CREATE TABLE #TEMPTABLE
(
Column1 type1,
Column2 type2,
Column3 type3
)
INSERT INTO #TEMPTABLE
SELECT ...
SELECT *
FROM #TEMPTABLE ...
DROP TABLE #TEMPTABLE
In Sqlite:
CREATE TABLE T AS
SELECT * FROM ...;
-- Use temporary table `T`
DROP TABLE T;

Resources