我的供应商送来了一段SQL代码,我正在经历一段地狱般的时间来处理我现有的查询。
我有一个在数据库表中查询多个值的网页,下面是我在ASP页面中查询SQL的代码:
SELECT [Account],
[AccountName],
[ComputerName],
[EMail],
[Telephone],
[Status],
cast(cast([StartDate] as float) as datetime) as 'Start-Date',
cast(cast([LastBackupDate] as float) as datetime) as 'LastBackup-Date',
[TotalFileSize],
[TotalFileCount]
FROM ExpandedView WHERE loginid = @0这个查询运行得很好,但是上面指定的" account“字段显然只返回实际帐号的一部分。显然,我的供应商使用计算来创建最终帐号,并将其发送给我:
use <databasenamehere>
select left(
convert(nvarchar,E.Account)
,5)+'-'+convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))
%10 )
+right(convert(nvarchar,E.Account),4)
as 'Account', AccountName from ExpandView E
Inner Join Customer C
on E.Account=C.Account现在我意识到,这个查询选择了账号的前5个数字,附加了连字符,然后对最后4个数字执行了一个数学过程,但是我不知道如何阅读它。我可以简单地将这两个语句与"AND“子句附加在一起吗?
我真的很想了解"E.Account“指的是什么,以及为什么他们在查询中使用"ExpandView E”和"Customer C“。我还有点搞不懂这些和实际的表有什么关系。
任何帮助都将不胜感激。或者,如果你愿意,发布一个链接,我会尽我所能地阅读。
谢谢!
发布于 2013-01-29 10:10:22
下面是我发现的一个似乎可以工作的查询:
select left
(convert(nvarchar,E.Account)
,5)+'-'+ convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))%10 )
+right(convert(nvarchar,E.Account),4)
as 'Account',
E.AccountName,
E.ComputerName,
E.regloginid,
E.EMail,
E.Telephone,
E.Community,
E.Status,
CAST(cast(e.startdate as float) as datetime) as 'Start-Date',
CAST(cast(e.lastbackupdate as float) as datetime) as 'LastBackup-Date',
E.TotalFileSize,
E.TotalFileCount
from ExpandedCustomerView E Where E.Status = 'A' and E.regloginid = @0 他们使用的查询似乎重命名了数据库表,以E.xxxxx作为前缀,我不确定这是如何工作的。我对MSSQL不是很熟悉,无法理解他们为什么要在select中调用参数的重命名。但是,这个查询可以正常工作,并提供了我需要的数据。
也许有人可以回答为什么他们会在"FROM EXPANDEDVIEW E“部分使用参数?这似乎是列的重命名发生的点。
无论如何,我很有兴趣知道,但这对我来说肯定是有效的,所以我要结束这一点。感谢大家的回答/评论和帮助。
发布于 2013-01-25 08:07:36
这应该是可行的:
SELECT left(
convert(nvarchar,E.Account)
,5)+'-'+convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))
%10 )
+right(convert(nvarchar,E.Account),4)
as 'Account',
[AccountName],
[ComputerName],
[EMail],
[Telephone],
[Status],
cast(cast([StartDate] as float) as datetime) as 'Start-Date',
cast(cast([LastBackupDate] as float) as datetime) as 'LastBackup-Date',
[TotalFileSize],
[TotalFileCount]
FROM ExpandedView E WHERE loginid = @0https://stackoverflow.com/questions/14513105
复制相似问题