我在存储过程中有一个char(8)变量格式化为ddmmyyyy(此值的质量和有效性未知且超出我的控制范围).将值移动到datetime变量的最有效方法是什么,如果它不是有效的datetime则抛出错误.
DECLARE @Source char(8)
DECLARE @Destination datetime
SET @Source='07152009'
--your solution here
SELECT @Destination
Run Code Online (Sandbox Code Playgroud)
这是我能想到的最佳方式:
DECLARE @Source char(8)
DECLARE @Temp varchar(10)
DECLARE @Destination datetime
set @Source='07152009'
SET @Temp=LEFT(@Source,2)+'/'+SUBSTRING(@Source,3,2)+'/'+RIGHT(@Source,4)
IF ISDATE(@Temp)!=1
BEGIN
RAISERROR('ERROR, invalid date',16,1)
END
SET @Destination=@Temp
SELECT @Source AS Source, @Temp AS Temp, @Destination AS Destination
Run Code Online (Sandbox Code Playgroud)
编辑这里是我要去的...
DECLARE @Source char(8)
DECLARE @Destination datetime
set @Source='07152009'
BEGIN TRY
SET @Destination=CONVERT(datetime,RIGHT(@Source,4) -- YYYY
+LEFT(@Source,2) -- MM
+SUBSTRING(@Source,3,2) -- DD
)
END TRY
BEGIN CATCH
PRINT 'ERROR!!!' --I'll add a little …Run Code Online (Sandbox Code Playgroud) 当我运行此查询时
SELECT CustomerId FROM Stocks.dbo.Suppliers
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误.列名称'CustomerId'无效.此错误有效,因为Suppliers表中没有列CustomerId; 但是当我在子查询中使用相同的查询时,它不会给出任何错误.例如
SELECT *
FROM SomeOtherDb.dbo.Customer
WHERE CustomerId In( SELECT CustomerId
FROM Stocks.dbo.Suppliers)
Run Code Online (Sandbox Code Playgroud)
在这里,我期待相同的错误"无效的列名称",但查询运行没有任何错误.
完全限定名称只是惯例,两个dbs都在同一台服务器上.
CustomerId确实存在于SomeOtherDb.dbo.Customer表中,但不存在于子查询中.
为什么会这样?这是子查询的东西吗?
谢谢.
以下哪个查询更好...这只是一个例子,有很多情况,我希望显示用户名而不是UserID
Select EmailDate, B.EmployeeName as [UserName], EmailSubject
from Trn_Misc_Email as A
inner join
Mst_Users as B on A.CreatedUserID = B.EmployeeLoginName
Run Code Online (Sandbox Code Playgroud)
要么
Select EmailDate, GetUserName(CreatedUserID) as [UserName], EmailSubject
from Trn_Misc_Email
Run Code Online (Sandbox Code Playgroud)
如果使用First没有性能优势,我宁愿使用第二个...我会在用户表中有大约2000条记录,在电子邮件表中有100k条记录...
谢谢
sql-server performance sql-server-2005 inner-join user-defined-functions
我的理解是,当在SELECT语句中使用NOLOCK时,它也可以读取未提交的/脏行.但我想利用表上的NOLOCK提示,以便我的SELECT查询能够快速运行.
现在,NOLOCK在桌上但是与"SET TRANSACTION ISOLATION LEVEL READ COMMITTED"一起给我NOLOCK优势和更快的SELECT查询(因为NOLOCK)只有提交的行(因为SET)?
我知道MS SQL Server中的存储过程.但我对MS SQL Server中的扩展存储过程没有一个明确的想法.它是什么?用一个简单的例子来解释它.谢谢.
在主键列上连接两个简单表并放置加法相等条件时,可以在join本身或where子句中完成.
例如,以下是等价的.我的问题是 - 有没有理由使用一种风格而不是另一种风格?
SELECT *
FROM A
INNER JOIN B ON A.A_ID = B.A_ID
AND A.DURATION = 3.00
Run Code Online (Sandbox Code Playgroud)
... VS:
SELECT *
FROM A
INNER JOIN B ON A.A_ID = B.A_ID
WHERE A.DURATION = 3.00
Run Code Online (Sandbox Code Playgroud) 为什么必须在一个表的列上具有主键,而另一个表的列具有外键引用.
create table D(Did int)
create table E(Eid int foreign key references D(Did))
Run Code Online (Sandbox Code Playgroud)
上面的查询给出了错误:
There are no primary or candidate keys in the referenced table 'D' that match
the referencing column list in the foreign key 'FK__E__Eid__79C80F94'.
Run Code Online (Sandbox Code Playgroud) 我正在使用MS SQL Server 2008,并且在表中我将列idate作为日期但是以整数格式.但在查询中我希望以日期格式.是否可以将整数日期转换为正确的日期时间格式?
我知道这个问题与这个问题非常相似: 对称交叉连接 也是这个问题: 在sql中交叉连接的组合(不是排列)
但是,如果我们有两个不同的表,比如说A和B:
select A.id,B.id from A cross join B
Run Code Online (Sandbox Code Playgroud)
而且我想考虑一对(a,b)等于(b,a)?
我正在使用SQL Server 2005.我创建了一个大部分时间都可以工作的存储过程,但是我找到了一个不能满足我想要的实例.
目前,代码执行类似的操作
if @@error <> 0
begin
select @message_error = "There was a database error adding product "+ @product + " to product line
end
Run Code Online (Sandbox Code Playgroud)
@message_error输出变量在哪里.
所以,我可以select @@error得到一个数字,但我真正想要的只是SQL错误.
像嘿这样的东西,我不能这样做,因为这个列上有一个fk约束或者其他什么.我在msdn上找到了这篇文章 http://msdn.microsoft.com/en-us/library/ms178592(v=sql.90).aspx
但它只会抛出自定义异常RAISERROR,我不想创建自己的错误消息或异常,我只是想知道为什么东西不起作用.我可以通过Management Studio执行存储过程并查看确切的SQL错误,但尝试匹配来自站点的数据并手动插入它是很乏味的.
如何将SQL错误文本转换为输出变量?
sql-server-2005 ×10
sql ×7
sql-server ×6
t-sql ×5
foreign-keys ×1
inner-join ×1
key ×1
performance ×1
primary-key ×1
subquery ×1