Using results from one query as the parameters for another

Is there a way to have a SQL query automatically update its' parameters based on the results of a previous query?

Here's the scenario I'm trying to achieve:

A report should automatically be run daily that runs the following queries (in order):

  1. SELECT id of rows that match a certain criteria (we'll call this result set "X")
  2. SELECT certain fields from rows that are titled "Cancelled-[X]"
  3. UPDATE rows from [X] with information from "Cancelled-[X]"

Replay

Use local Temp Table in your procedure:

SELECT id INTO #X
FROM ABC WHERE MyCriteria = 'XYZ';

SELECT * FROM ABC
WHERE Cancelled = 'Yes' and
    id in (SELECT id FROM #X);

UPDATE ABC SET MyColumn = 'Updated'
WHERE Cancelled = 'Yes' and
    id in (SELECT id FROM #X);

Category: sql server Time: 2016-07-29 Views: 3

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.129 (s). 12 q(s)