1

例如,我的 sql 命令返回这个

 01234567

那么我如何能够插入文本,以便在语句末尾返回它。所以如果我返回多个值,那么 TEXT 只会出现在最后。

 01234567TEXT

我似乎无法在网上找到太多的帮助,但我知道这是可能的。我当前的sql命令如下:

 SqlCommand command = new SqlCommand("SELECT PID = RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) FROM dbo.MDRMASTER WHERE PARENTPID = @PARENTPID",con);

任何帮助或指向正确方向的方法都会很棒。非常感谢

更新我一开始就用错了这个问题。我要在最后一个陈述的末尾说。感谢 Siva 澄清这一点。不过,我确实对每个人都竖起了大拇指。感谢大家的帮助。

4

6 回答 6

4

您只需要在结果末尾连接您的字符串:

添加:

+ 'TEXT'

sql命令将是

 SqlCommand command = new SqlCommand("SELECT PID = RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) + 'TEXT' FROM dbo.MDRMASTER WHERE PARENTPID = @PARENTPID",con);

Based on your Edit I think you only want the TEXT appended to the final record. If so, then you could do the following:

create table temp
(
    field varchar(10)
)

insert into temp values ('12345678')
insert into temp values ('23456789')
insert into temp values ('34567890')
insert into temp values ('45678901')
insert into temp values ('56789012')

select field
from temp
WHERE field != (SELECT Max(field) from temp)
UNION
select field + 'TEXT'
from temp
WHERE field = (SELECT Max(field) from temp)

drop table temp

If you just need the TEXT as the last row then you would do:

create table temp
(
    field varchar(10)
)

insert into temp values ('12345678')
insert into temp values ('23456789')
insert into temp values ('34567890')
insert into temp values ('45678901')
insert into temp values ('56789012')

select field
from temp
UNION
select 'TEXT'

drop table temp

Your SQLCommand would be

SqlCommand command 
    = new SqlCommand("SELECT PID = RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) 
                      FROM dbo.MDRMASTER 
                      WHERE PARENTPID = @PARENTPID 
                      UNION 
                      SELECT 'TEXT'",con);
于 2012-05-02T13:45:55.950 回答
4

If database value is a string, concatenation is as simple as this...

SELECT T.col + 'TEXT'
FROM Table AS T

If database value is not a string, you might need to cast before concatenating, like this...

SELECT CAST(T.col AS nvarchar)  + 'TEXT'
FROM Table AS T

From your code, it would be more like this...

SqlCommand command = new SqlCommand("SELECT RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) + 'TEXT' AS PID FROM dbo.MDRMASTER WHERE PARENTPID = @PARENTPID", con);
于 2012-05-02T13:47:23.733 回答
3

您可以在 SQL 中附加文本 -

SqlCommand command = new SqlCommand("SELECT PID = RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) + 'TEXT' FROM dbo.MDRMASTER WHERE PARENTPID = @PARENTPID",con);
于 2012-05-02T13:45:44.020 回答
2

You just have to use the "+" operator in your SQL SELECT statement:

SELECT PID = RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) + 'TEXT'

This would end up making your code look like this:

SqlCommand command = new SqlCommand(@"SELECT PID = RIGHT(REPLICATE('0',8) + CAST(PID AS VARCHAR(8)),8) 
                                      FROM dbo.MDRMASTER 
                                      WHERE PARENTPID = @PARENTPID"
    ,con);

See the MSDN article on String concatenation in Transact SQL.

于 2012-05-02T13:46:03.843 回答
2

If I understand correctly, you need the text at the end of the result. Then you need to perform a UNION.

Code:

SqlCommand command = new SqlCommand("
SELECT PID = RIGHT(REPLICATE('0',8) + 
CAST(PID AS VARCHAR(8)),8) 
FROM dbo.MDRMASTER 
WHERE PARENTPID = @PARENTPID 
UNION SELECT 'TEXT'",
con);
于 2012-05-02T13:55:57.720 回答
0

If you want to input a text, for example "text" then just do this:

select 'text'

You can then add it above or below the result of your actual query with UNION

于 2017-03-30T05:42:39.473 回答