C# 如何从SQL Server获取大于多列值的值,而不是使用复合键字段
如果我有包含以下数据的表: branch,type,number=>这将生成复合键字符串“keyfield” 分支的长度,类型为[int][4]C# 如何从SQL Server获取大于多列值的值,而不是使用复合键字段,c#,sql,sql-server,C#,Sql,Sql Server,如果我有包含以下数据的表: branch,type,number=>这将生成复合键字符串“keyfield” 分支的长度,类型为[int][4] 数字长度为[int][7] 数据如下: branch, type, number 13, 1309, 1 row1 13, 1309, 2 row2 13, 1310, 1 row3 14, 1309, 1 row4 SELECT TOP 1 * FROM [TABLE] WHE
数字长度为[int][7] 数据如下:
branch, type, number
13, 1309, 1 row1
13, 1309, 2 row2
13, 1310, 1 row3
14, 1309, 1 row4
SELECT TOP 1 * FROM [TABLE]
WHERE dbo.GetCompositeKey(branch, type, number) >
dbo.GetCompositeKey(13, 1309, 2)
ORDER BY dbo.GetCompositeKey(branch, type, number)
SELECT TOP 1 * FROM [myview]
WHERE CompositeKey > dbo.GetCompositeKey(13, 1309, 2)
ORDER BY CompositeKey
所以我有keyfield->称为“keyfield”列,但我不需要使用它,它可以工作,但我只需要使用表达式而不是字符串keyfield
例如:如果我需要获得大于上面第2行的行: 我写道:
SELECT TOP 1 * FROM TABLE WHERE KeyField > '0013130900002'
-->我不喜欢使用字符串作为复合键
我也不能像这样手动操作:
SELECT TOP 1 * FROM TABLE WHERE brn > 1 AND type > 1309 and num > 2
这是行不通的。。。所以我需要通过表达式得到下一行
例如:getGreatRow(11309,2);//这将返回第3行,即我需要执行的操作。
因此,此功能可以直接与C#和屏幕上的文本框一起使用!!
我需要选择唯一一条大于我指定的当前记录或表达式值的前1条记录
编辑
我使用Gordon SQL生成了一个C#,其中包含我想要的主键列表。多亏了戈登
在C#中,要自动生成SQL查询:
public List<EntryTable> Tables { get; private set; }
public List<BufferElement> Buffer { get; private set; }
string Query = string.Empty;
for (int i = 0; i < Tables[0].PrimaryKeys.Count; i++)
{
Query += "(";
for (int j = 0; j < i; j++)
{
switch (Tables[0].PrimaryKeys[j].CLRType)
{
case CLRType.CLR_BYTE:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToByte(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_INT16:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToInt16(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_INT32:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToInt32(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_INT64:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToInt64(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_SINGLE:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToSingle(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_DOUBLE:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToDouble(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_DECIMAL:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToDecimal(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_Boolean:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = {Convert.ToBoolean(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)} AND ";
break;
case CLRType.CLR_STRING:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = '{Convert.ToString(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)}' AND ";
break;
case CLRType.CLR_DATETIME:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = '{Convert.ToDateTime(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)}' AND ";
break;
case CLRType.CLR_TIME:
Query += $"{Tables[0].PrimaryKeys[j].KeyPart} = '{TimeSpan.Parse(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[j].KeyPart).Value)}' AND ";
break;
}
}
switch (Tables[0].PrimaryKeys[i].CLRType)
{
case CLRType.CLR_BYTE:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToByte(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_INT16:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToInt16(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_INT32:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToInt32(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_INT64:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToInt64(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_SINGLE:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToSingle(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_DOUBLE:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToDouble(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_DECIMAL:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToDecimal(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_Boolean:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > {Convert.ToBoolean(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}";
break;
case CLRType.CLR_STRING:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > '{Convert.ToString(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}'";
break;
case CLRType.CLR_DATETIME:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > '{Convert.ToDateTime(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}'";
break;
case CLRType.CLR_TIME:
Query += $"{Tables[0].PrimaryKeys[i].KeyPart} > '{TimeSpan.Parse(Buffer.Find(x => x.ID == Tables[0].PrimaryKeys[i].KeyPart).Value)}'";
break;
}
Query += $") {(Tables[0].PrimaryKeys.Count > 1 && i != Tables[0].PrimaryKeys.Count - 1 ? " OR " : string.Empty)} \n";
}
Query += $"ORDER BY {string.Join(" ASC, ", Tables[0].PrimaryKeys.Select(x => x.KeyPart).ToArray())} ASC";
SelectCommand = $"SELECT TOP 1 * FROM {Tables[0].Table} WHERE " + Query;
公共列表表{get;private set;}
公共列表缓冲区{get;private set;}
string Query=string.Empty;
对于(int i=0;i<表[0]。PrimaryKeys.Count;i++)
{
查询+=“(”;
对于(int j=0;jx.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
案例CLRType.CLR\U INT16:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToInt16(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
案例CLRType.CLR_INT32:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToInt32(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
案例CLRType.CLR\U INT64:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToInt64(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
案例CLRType.CLR\u单个:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToSingle(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
大小写CLRType.CLR\u DOUBLE:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToDouble(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
大小写CLRType.CLR\U DECIMAL:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToDecimal(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
大小写CLRType.CLR\U布尔值:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}={Convert.ToBoolean(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}和”;
打破
大小写CLRType.CLR_字符串:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}='{Convert.ToString(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}”和“;
打破
案例CLRType.CLR\U日期时间:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}='{Convert.ToDateTime(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}”和“;
打破
案例CLRType.CLR\U时间:
Query+=$“{Tables[0].PrimaryKeys[j].KeyPart}='{TimeSpan.Parse(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[j].KeyPart.Value)}”和“;
打破
}
}
开关(表[0].PrimaryKeys[i].CLRType)
{
大小写CLRType.CLR_字节:
Query+=$“{Tables[0].PrimaryKeys[i].KeyPart}>{Convert.ToByte(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[i].KeyPart.Value)}”;
打破
案例CLRType.CLR\U INT16:
Query+=$“{Tables[0].PrimaryKeys[i].KeyPart}>{Convert.ToInt16(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[i].KeyPart.Value)}”;
打破
案例CLRType.CLR_INT32:
Query+=$“{Tables[0].PrimaryKeys[i].KeyPart}>{Convert.ToInt32(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[i].KeyPart.Value)}”;
打破
案例CLRType.CLR\U INT64:
Query+=$“{Tables[0].PrimaryKeys[i].KeyPart}>{Convert.ToInt64(Buffer.Find(x=>x.ID==Tables[0].PrimaryKeys[i].KeyPart.Value)}”;
打破
案例CLRType.CLR\u单个:
Query+=$“{Tables[0].PrimaryKeys[i].KeyPart}>{Con
123456 99349199 12341234 : having enough space for a 'reasonable' amount of numbers
000000 00000000 00000000
so 123456 x 1 00000000 00000000 = 123456 00000000 00000000 +
99349199 x 1 00000000 = 99349199 00000000 +
and
12341234 x 1 = 12341234 =
-------------------------
123456 99349199 12341234
select *
from my_table
WHERE new_id (branch)*1 00000000 00000000+(type)*1 00000000+number
new_label= (branch)*100000000 00000000+(type)*100000000 + number
<all digits you want> (branch) + 8 digit (type) + 8 digit (number)
CREATE FUNCTION dbo.GetCompositeKey(@branch int, @type int, @number int)
RETURNS bigint
AS
BEGIN
RETURN cast(@branch as bigint) * 100000000000 + cast(@type as bigint) * 10000000 + @number
END
SELECT TOP 1 * FROM [TABLE]
WHERE dbo.GetCompositeKey(branch, type, number) >
dbo.GetCompositeKey(13, 1309, 2)
ORDER BY dbo.GetCompositeKey(branch, type, number)
CREATE VIEW [myview]
AS
SELECT
*,
dbo.GetCompositeKey(branch, type, number) as CompositeKey
FROM [table]
SELECT TOP 1 * FROM [myview]
WHERE CompositeKey > dbo.GetCompositeKey(13, 1309, 2)
ORDER BY CompositeKey