Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

The condition in the SQL Server based on the first value of the table column is ...

$
0
0

I am new in SQL Queries..

I have 2 database DB1 & DB2.

DB1 Contains the table A which has Name,Address & Code columns & DB2 contains the table B which has column Name1,Address1.

if the Code is not null then i want to select Name,Address from the tableA else name1 & address1 should come from tableB.

Please Help me on this.

select top 1 a.name 'MyName', a.address 'MyAddress' from DB1.table1 a where a.code <> null Assuming your tableA and TableB have primary key as "Name" select case when a.Code is not null then a.Name else b.Name end as Name ,case when a.Code is not null then a.Address else b.Address end as Address from DB1..tableA a left join DB1..tableB b on a.Name=b.name

OR

select case when isnull(a.Code,'')<>'' then a.Name else b.Name end as Name ,case when isnull(a.Code,'')<>'' then a.Address else b.Address end as Address from DB1..tableA a left join DB1..tableB b on a.Name=b.name


Viewing all articles
Browse latest Browse all 3160

Latest Images

Trending Articles



Latest Images