我可以在视图中连接虚拟列吗?
不幸的是,我在数据库中有明文密码。我想尽可能少地传递这些明文值,例如比较和更新。为此,我尝试创建用户表的视图,该视图不包括明文密码,而是提供该密码的哈希值。
这是我当前的 SQL Server 视图,它不起作用:
SELECT CAST(CAST(32768 * RAND() AS INT) AS NVARCHAR) AS PasswordSalt
HashBytes('SHA1', PasswordSalt + u.Password) AS PasswordHash
FROM dbo.Users AS u
我很高兴听到此方法的替代方案,但否则问题似乎是将虚拟列 PasswordSalt 与......任何内容连接起来。例如,以下简单视图可以工作:
SELECT u.Login AS Column1, u.Login + 'b' AS Column2
但这个视图不行:
SELECT u.Login AS Column1, Column1 + 'b' AS Column2
我从 Management Studio 收到的错误是
列名“Column1”无效。
预先感谢您对我做错了什么的任何想法!
Unfortunately, I have plaintext passwords in a database. I want to pass these plaintext values around as little as possible for, say, comparisons and updates. To this end, I'm trying to create a view of my Users table that excludes the plaintext passwords and instead provides a hashed value of that password.
Here's my current SQL Server view, which doesn't work:
SELECT CAST(CAST(32768 * RAND() AS INT) AS NVARCHAR) AS PasswordSalt
HashBytes('SHA1', PasswordSalt + u.Password) AS PasswordHash
FROM dbo.Users AS u
I'd be happy to hear about alternatives to this approach, but otherwise the problem seems to be concatenating the virtual column PasswordSalt with.. anything. For instance, the following simple view works:
SELECT u.Login AS Column1, u.Login + 'b' AS Column2
but this one does not:
SELECT u.Login AS Column1, Column1 + 'b' AS Column2
The error I'm receiving from Management Studio is
Invalid column name 'Column1'.
Thanks in advance for any ideas about what I'm doing wrong!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出现此问题的原因是语句的 FROM 子句指示要选择的数据来自 Users 表,但 SELECT 部分引用了名为 PasswordSalt 的列。 SQL Server 在 Users 表中找不到具有此名称的列,因此出现错误。
替代方法可能是在子查询中生成 Salt。例如
SELECT x.PasswordSalt,
HashBytes('SHA1', x.PasswordSalt + x.Password) AS PasswordHash
FROM ( SELECT CAST(CAST(32768 * RAND() AS INT) AS NVARCHAR) AS PasswordSalt,
密码
来自 dbo.Users) x
The problem is occurring because the FROM clause of your statement indicates that the data to be selected comes from the Users table, but the SELECT part references a column named PasswordSalt. SQL Server cannot find a column with this name on the Users table, hence the error.
Alternative approach may be to generate the Salt in a subquery. For example
SELECT x.PasswordSalt,
HashBytes('SHA1', x.PasswordSalt + x.Password) AS PasswordHash
FROM ( SELECT CAST(CAST(32768 * RAND() AS INT) AS NVARCHAR) AS PasswordSalt,
Password
FROM dbo.Users) x
怎么样
What about