开发者

Selecting columns in SQL

I have two tables and the following query:

table1
---------
table1Id(pk)
fromdate,
todate,
name,
link

table2
--------
table2Id(pk)
url
table1Id(fk)

SELECT  @ITEM = table1Id FROM table1
    WHERE 
        table1.FromDate <= @ToDate AND  @FromDate <= table1.ToDate  


select * from table2 where table1Id =@ITEM

Is it possible to select the other fields from Table1 as well?开发者_JAVA技巧


Yes surely possible -

DECLARE @item1 nvarchar(100)
DECLARE @item2 nvarchar(200)

select @item1 = <column1>, @item2 = <column2> from <table>

PRINT @item1
PRINT @item2


Sure, do you mean something like this?

Select 
   @Item = table1Id, 
   @Item2 = table2.Column1, 
   @Item3 = table2.Column2 
From Table1
Inner Join table2 on table1.table1Id = table2.table1Id
...


Yes, you can do multiple selections, SELECT item1, item2 FROM table1 etc.


If I understand you correctly, then yes, you can assign multiple values.

DECLARE @table1Id INT,
        @fromdate DATETIME,
        @todate DATETIME, 
        @name VARCHAR(20), 
        @link VARCHAR(20)

SELECT  @table1Id = table1Id
        @fromdate = fromdate, 
        @todate = todate, 
        @name = name, 
        @link = link
FROM    table1
WHERE   table1.FromDate <= @ToDate 
AND     @FromDate <= table1.ToDate
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜