在 SQL Server 2000 中连接结果集中的列

发布于 2024-07-26 09:38:40 字数 318 浏览 2 评论 0原文

我有一个以以下形式返回数据的查询

attribute       value
---------      ----------
petid           1000
name            buttercup
species         cat
age             10
owner           Bob Dole

基本上我想遍历每一行并在单个字符串中返回字段名称和物种,所以这里的结果将是

buttercup cat

我有什么想法可以做到这一点吗?

I have a query that returns data in the following form

attribute       value
---------      ----------
petid           1000
name            buttercup
species         cat
age             10
owner           Bob Dole

Basically I want to go through every row and return the fields name and species in a single string, so here the result would be

buttercup cat

Any ideas how I could do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

述情 2024-08-02 09:38:40

尝试这个。 我只在 SQL Serer 2008 上尝试过,但也许它会起作用:

DECLARE @Concat nvarchar(50)
SET @Concat=N''

SELECT @Concat = @Concat + Value + N' '
FROM dbo.AttributeValue
WHERE Attribute IN (N'name', N'species')

SELECT @Concat

Try this. I've only tried it with SQL Serer 2008, but maybe it will work:

DECLARE @Concat nvarchar(50)
SET @Concat=N''

SELECT @Concat = @Concat + Value + N' '
FROM dbo.AttributeValue
WHERE Attribute IN (N'name', N'species')

SELECT @Concat
方圜几里 2024-08-02 09:38:40

好的 - 现在我想我理解了数据格式...

这是创建示例集的代码(只是为了确保我正确无误)

CREATE TABLE MyTable
(
attribute varchar(20),
value varchar(20)
)

INSERT INTO MyTable VALUES('petid','1000')
INSERT INTO MyTable VALUES('name','buttercup')
INSERT INTO MyTable VALUES('species','cat')
INSERT INTO MyTable VALUES('age','10')
INSERT INTO MyTable VALUES('owner','Bob Dole')

这是我的答案:

SELECT a.value + ' ' +b.value
FROM MyTable AS a
INNER JOIN MyTable AS b ON a.attribute='name' AND b.attribute = 'species'

Okay - Now I think I understand the data format...

Here is the code to create the sample set (just to make sure I've got it right)

CREATE TABLE MyTable
(
attribute varchar(20),
value varchar(20)
)

INSERT INTO MyTable VALUES('petid','1000')
INSERT INTO MyTable VALUES('name','buttercup')
INSERT INTO MyTable VALUES('species','cat')
INSERT INTO MyTable VALUES('age','10')
INSERT INTO MyTable VALUES('owner','Bob Dole')

Here is my answer:

SELECT a.value + ' ' +b.value
FROM MyTable AS a
INNER JOIN MyTable AS b ON a.attribute='name' AND b.attribute = 'species'
半衾梦 2024-08-02 09:38:40

执行此操作的光标方式将是这样的 -

DECLARE @name varchar(20) 
DECLARE @species varchar(20)    
DECLARE nameSpeciesCursor CURSOR FOR 
SELECT name, species FROM tableName

OPEN nameSpeciesCursor 
FETCH NEXT FROM nameSpeciesCursor INTO @name, @species  

WHILE @@FETCH_STATUS = 0  
BEGIN  
       PRINT @name + ' ' + @species    
       FETCH NEXT FROM nameSpeciesCursor INTO @name, @species  
END  

CLOSE nameSpeciesCursor 
DEALLOCATE nameSpeciesCursor 

干杯

The cursor way of doing this would be some thing like this-

DECLARE @name varchar(20) 
DECLARE @species varchar(20)    
DECLARE nameSpeciesCursor CURSOR FOR 
SELECT name, species FROM tableName

OPEN nameSpeciesCursor 
FETCH NEXT FROM nameSpeciesCursor INTO @name, @species  

WHILE @@FETCH_STATUS = 0  
BEGIN  
       PRINT @name + ' ' + @species    
       FETCH NEXT FROM nameSpeciesCursor INTO @name, @species  
END  

CLOSE nameSpeciesCursor 
DEALLOCATE nameSpeciesCursor 

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文