1

I have a simple table as follows

CREATE TABLE [accounting].[ExtractControl](
    [SourceSchema] [varchar](50) NOT NULL,
    [SourceTable] [varchar](150) NOT NULL,
    [SourceDatabase] [varchar](50) NOT NULL)

I would like to select SourceDatabase using SourceSchema and SourceTable values

Currently I am doing with multiple queries as follows

Select @s1 = SourceDatabase from accounting.ExtractControl where SourceSchema = 'xxx' and SourceTable = 'xxx'
Select @s2 = SourceDatabase from accounting.ExtractControl where SourceSchema = 'yyy' and SourceTable = 'yyy'
Select @s3 = SourceDatabase from accounting.ExtractControl where SourceSchema = 'xxx' and SourceTable = 'yyy'

I do believe that this can be done in more elegant ways! Thanks for the help!

2
  • 1
    What do you find not elegant with your current solution? Commented Dec 13, 2012 at 10:36
  • @DanielHilgarth I am querying the database 3 times to return 3 variables, I am thinking that maybe its possible to return all 3 variables using 1 query against the Db. Commented Dec 13, 2012 at 10:46

4 Answers 4

1

Are you asking for something like this?

select SourceDatabase from accounting.ExtractControl
where SourceSchema in ('xxx', 'yyy') and SourceTable in ('xxx', 'yyy');

This will return the SourceDatabase where the SourceSchema is either 'xxx' or 'yyy' and the SourceTable is either 'xxx' or 'yyy'.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Daniel It would indeed return SourceDatabase but I want them to be stored in different variables depending on SourceSchema and SourceTable For eg. s1 should be when SourceSchema and SourceTable are both 'xxx' s2 -> when both are 'yyy'... etc etc
If you actually need three different variables, go with your current solution.
@dopplesoldner: Agree with Daniel: if you want the three results in three distinct variables, stay with your current solution. However, if you elaborate more on how exactly you are going to use those variables, it may turn out there's a better way (or it may not).
1

option with one query

SELECT @s1 = MAX(CASE WHEN SourceSchema = 'xxx' and SourceTable = 'xxx' THEN SourceDatabase END),
       @s2 = MAX(CASE WHEN SourceSchema = 'yyy' and SourceTable = 'yyy' THEN SourceDatabase END),
       @s3 = MAX(CASE WHEN SourceSchema = 'xxx' and SourceTable = 'yyy' THEN SourceDatabase END)
FROM accounting.ExtractControl

Demo on SQL Fiddle

Comments

0
select * from accounting.ExtractControl
where SourceSchema = 'xxx' or SourceSchema = 'yyy'

2 Comments

Hi Mert I want them to be stored in different variables depending on SourceSchema and SourceTable For eg. s1 should be when SourceSchema and SourceTable are both 'xxx' s2 -> when both are 'yyy'... etc etc
I dont think there is better way then. you may select like this and linq in your code.
0

what about this Query...

select SourceDatabase
from accounting.ExtractControl
where SourceSchema in ('xxx', 'yyy')
      and SourceTable in ('xxx', 'yyy')
      and SourceTable = SourceSchema

i did not tried this but i think this is what you required

1 Comment

if you want each cell value to different varriable then go with cursor now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.