SQL Server : stored procedure add table content to another table - sql

How can I add the results from a query by parameter to another table using a stored procedure?
Here is my select query text:
SELECT
dbo.PortfolioH.Epic,
dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
FROM
dbo.PortfolioH
LEFT OUTER JOIN
dbo.UserAccounts ON dbo.PortfolioH.UserN = dbo.UserAccounts.UserN
GROUP BY
dbo.PortfolioH.Epic,
dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
HAVING
(dbo.PortfolioH.AlertRatings = N'yes')
I want to append to table AlertEmails where dbo.PortfolioH.Epic = #Epic
any help greatly appreciated.

Just use the standard INSERT.....SELECT construct, which takes the data to be inserted from a query, something like this:
INSERT INTO dbo.AlertsEmails (Epic, AlertRatings, Email, UserN, AlertType)
SELECT dbo.PortfolioH.Epic, dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email, dbo.PortfolioH.UserN, 1 As AlertType
FROM dbo.PortfolioH LEFT OUTER JOIN
dbo.UserAccounts ON dbo.PortfolioH.UserN = dbo.UserAccounts.UserN
GROUP BY dbo.PortfolioH.Epic, dbo.PortfolioH.AlertRatings, dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
HAVING (dbo.PortfolioH.AlertRatings = N'yes') ;
It just runs the query, but instead of returning a result set, inserts the resulting rows into the table. Note that I've added the AlertType column in the INSERT part and added a fixed value in the SELECT part, as they must match exactly.

Related

Select output query with specific format using SQL Server

I need, if possible, a T-SQL query that will return the values in a specific format from the columns.
I have two tables:
The first table is PrimeClosureInformation table
[This is dbo.PrimeClosureInformation table]
And the second table is dbo.Event:
I did join with two table to get All the data for them Where This condition
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
but it returns the result like this:
Here is the result i want to get it
The format I need it for every PrimeClosureInformationPK record all events comments in one record or in one line like this example
PrimeClosureInformationPK Invoiced Remarks EventCommentFirst EventCreationDateTimeFirst EventCommentSecond EventCreationDateTimeSecond
And so on
This is the join I did it with the image for the result
this is the result of join i was did
SELECT TOP 1000
dbo.PrimeClosureInformation.PrimeClosureInformationPK,
dbo.PrimeClosureInformation.Invoiced,
dbo.PrimeClosureInformation.Remarks,
dbo.Event.EventComment,
dbo.Event.EventCreationDateTime,
FROM
dbo.PrimeClosureInformation
INNER JOIN
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
if I understand it correclty all values of one PK should be display in one column, than you can try this:
SELECT CONCAT(dbo.PrimeClosureInformation.PrimeClosureInformationPK, '|' , dbo.PrimeClosureInformation.Invoiced, '', dbo.PrimeClosureInformation.Remarks, '|'
dbo.Event.EventComment, '|'
dbo.Event.EventCreationDateTime
FROM
dbo.PrimeClosureInformation
INNER JOIN
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK

How to create a table with multiple columns

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.

how to do a update statement with a subquery?

I am trying to update a table using the information that exist from another table. the table that I want to update is called YVDtemp and the second table I am getting the information from is called yvs_textsMain. I also use a 3rd table to match any records that I dont have and this table is called YVD. Here is my update statement.
update Yvdtemp
set
NAME = yvd_textsmain.[name]
SPIC = yvd_textsmain.[rname]
EFFECT = yvd_textsmain.[desc]
where exists (
select yvd_textsMain.[name], yvd_textsMain.[rname], yvd_textsMain.[desc]
from YVD_textsMain
left outer join YVD
on (YVD_textsMain.[rname] = yvd.[spic])
where yvd.[spic] is null);
It seems that this statement is not working. It gives me an error "near 'spic': syntax error
but, when i use use the select statement apart from the update statement the select statement works and I get all the information that is not in YVD table and that information I want to pass it into YVDTemp table.
I am using SQLite.
As there is no matching condition between Yvdtemp and yvd_textsmain how will you map the rows in update statement.
Based on the requirement it looks like you are looking out for an Insert command which can be written as:
INSERT INTO Yvdtemp (NAME, SPIC, EFFECT)
Select yvd_textsMain.[name], yvd_textsMain.[rname], yvd_textsMain.[desc]
from YVD_textsMain
left outer join YVD
on (YVD_textsMain.[rname] = yvd.[spic])
where yvd.[spic] is null
You miss the commas
NAME = yvd_textsmain.[name],
SPIC = yvd_textsmain.[rname],
EFFECT = yvd_textsmain.[desc]

Transpose Data coming from SQL Server query into columns

I have following query to get data from multiple tables
SELECT TOP (100) PERCENT
dbo.tblProduct.PK AS PRODUCT_PK, dbo.tblProduct.PRODUCT_NAME,
dbo.tblProductSubComponent.SUB_COMPONENT_PK,
dbo.tblComponent.COMPONENT_NAME, dbo.tblSubComponent.SUB_COMPONENT_NAME
FROM
dbo.tblProductComponent
RIGHT OUTER JOIN
dbo.tblComponent
INNER JOIN
dbo.tblSubComponent
INNER JOIN
dbo.tblComponentSubComponent ON dbo.tblSubComponent.PK = dbo.tblComponentSubComponent.SUB_COMPONENT_PK
ON dbo.tblComponent.PK = dbo.tblComponentSubComponent.COMPONENT_PK
ON dbo.tblProductComponent.COMPONENT_PK = dbo.tblComponent.PK
LEFT OUTER JOIN
dbo.tblProductSubComponent ON dbo.tblSubComponent.PK = dbo.tblProductSubComponent.SUB_COMPONENT_PK
FULL OUTER JOIN
dbo.tblProduct ON dbo.tblProductSubComponent.PRODUCT_PK = dbo.tblProduct.PK AND dbo.tblProductComponent.PRODUCT_PK = dbo.tblProduct.PK
ORDER BY
PRODUCT_PK
The result is
and now I want to show data in gridview like this
Never Mind.. I solved it myself. Created a stored procedure to fetch data and stored it in table variable and then used alter column query to add my rows as column of temporary table.
After creating the temporary table, I looped through each component and inserted record for each component and sub-component.

Azure stored procedure out value

I've been trying to assemble a stored procedure on my Azure database that when it runs the query, it returns one output value from one specific column.
The likelihood of multiple results is zero since the the table being queried has 3 columns, and the query must mach 2. Then it grabs data from another table. The key is I need the first query to output the value in order to commence the second query.
At present I have 2 procedures, I would like to have one.
Query is as such for the moment:
select
customers_catalogs_define.catalog_id
from
customers_catalogs
left outer join
customers_catalogs_define on customers_catalogs.catalog_id = customers_catalogs_define.catalog_id
where
customers_catalogs.catalog_unique_identifier = #catalog_unique
AND customers_catalogs_define.customer_id = #customer_id
The output of course is the catalog_id. From that I take it into another query which I have that does the actual list retrieval. At the very least I would like to add a line that simply states #catalog_id = output
Thanks
You have two options basically to have this work :
Since I haven't seen your second query, just make sure its querying the correct table listed below as CUSTOMERS_CATALOGS_DEFINE
Using a variable as you suggested:
DECLARE #CATALOG_ID INT
SET #CATALOG_ID = (
SELECT CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID
FROM CUSTOMERS_CATALOGS
LEFT OUTER JOIN CUSTOMERS_CATALOGS_DEFINE
ON CUSTOMERS_CATALOGS.CATALOG_ID = CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID
WHERE CUSTOMERS_CATALOGS.CATALOG_UNIQUE_IDENTIFIER = #CATALOG_UNIQUE
AND CUSTOMERS_CATALOGS_DEFINE.CUSTOMER_ID = #CUSTOMER_ID )
SELECT *
FROM CUSTOMERS_CATALOGS_DEFINE
WHERE CATALOG_ID = #CATALOG_ID
Second option would be to do it in one query:
SELECT *
FROM CUSTOMERS_CATALOGS_DEFINE
WHERE CATALOG_ID IN (
SELECT CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID
FROM CUSTOMERS_CATALOGS
LEFT OUTER JOIN CUSTOMERS_CATALOGS_DEFINE
ON CUSTOMERS_CATALOGS.CATALOG_ID = CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID
WHERE CUSTOMERS_CATALOGS.CATALOG_UNIQUE_IDENTIFIER = #CATALOG_UNIQUE
AND CUSTOMERS_CATALOGS_DEFINE.CUSTOMER_ID = #CUSTOMER_ID
)