我使用下面的SQL从Intranet中的文件导入一些数据.然而,每隔一段时间,就会出现超时错误并且proc会失败,这就是我使用事务的原因.如果事务失败,我希望清除ImportedTable.然而,这似乎并没有发生.这里有什么我想念的吗?
ALTER PROCEDURE [dbo].[pr_ImportData]
@StoreCode varchar(10),
@UserId varchar(100)
AS
BEGIN TRANSACTION
-- 1) Clear the data
exec pr_INTRANET_ClearData @StoreCode, @UserId
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
GOTO EXIT1
END
-- 2) Add the new data to the history Table
INSERT INTO data_History (...)
SELECT ... from ImportedTable WHERE StoreCode = @StoreCode and UserId = @UserId
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
GOTO EXIT1
END
-- 3) Add the data to the live table
INSERT INTO data_Live (...)
SELECT …Run Code Online (Sandbox Code Playgroud) 在表格中有113列.并且表中有两个默认记录,一个用于未知,另一个用于不适用.因此,每列都有自己的默认值来表示未知和不适用.
我不想写常规插入语句来获取这两个记录.
所以,我试图用光标插入每一列.
从information_schema.columns获取该表的列名,并尝试使用"insert into select"语句将精确表中的值插入另一个位置,但是我们从information_schema获取的列的名称
Declare @col_name varchar(50)
declare my_cur CURSOR for
select column_name from information_schema.columns
where table_name = 'tabl' and table_catalog = 'db'
and table_schema = 'dbo'
Fetch next from my_cur
into @col_name
while @@FETCH_STATUS = 0
BEGIN
Insert into db.dbo.tabl (***@col_name***)
select ***@col_name*** from openrowset('sqlncli', 'server=my_server; trusted_connection=yes;', db.dbo.tabl)
fetch next from my_cur into @col_name
end
close my_cur
deallocate my_cur
go
Run Code Online (Sandbox Code Playgroud)
但是,我没有意识到@col_name将被视为字符串,而不是对象(列)
对于这种情况或任何替代解决方案是否有任何解决方法.
我第一次使用SQL Server 2005,过去主要使用MySQL.我习惯使用auto_increment在表中创建唯一的ID.
无论如何......我正在使用java应用程序,需要执行以下操作.假设我的表有两列:itemID(int)和itemValue(int).
这基本上就是我想要做的(dbconn方法只是伪代码):
dbconn.execSQL("begin tran");
int nextID = dbconn.execSQLSelect("select max(itemID)+1 from itemTable");
dbconn.execSQLInsert("insert into itemTable values " + nextID + ", 1000");
dbconn.execSQL("commit tran");
Run Code Online (Sandbox Code Playgroud)
begin/commit tran语句是否会处理第2行和第3行之间可能的竞争条件?或者是否有一些TSQL相当于MySQL的"锁表",我需要做什么?
在编写C#代码时,如果我可以设法获得SqlConnection,有没有办法执行查询来获取数据库的大小?
我搜索了互联网,似乎可以使用"sp_spaceused",但它不是一个表,它是一个程序.
我可以查询程序吗?
我有一个需要1:20分钟才能执行的sql.它处理一年的数据,但即便如此,我觉得它需要太长时间.我根据另一个查询的建议改变了IN使用EXISTS(在这种情况下,优化不是:S)你有另一个优化它的建议吗?
select gd.descripcion,count(gd.descripcion) as total
from diagnosticos d,gruposdiagnosticos gd, ServiciosMedicos s, pacientes p,Actos a,historias h
where p.codigo=h.codpaciente and p.codigo=a.codpaciente and p.codigo=h.codpaciente and p.codigo=s.codpaciente and h.codpaciente=a.codpaciente and h.codpaciente=s.codpaciente and a.codpaciente=s.codpaciente and h.numhistoria=a.numhistoria and h.numhistoria=s.numhistoria and a.numacto=s.numacto and h.codseccion=a.codseccion and a.codseccion=s.codseccion and d.codigo=s.codDiagnostico and gd.codigo=d.codgrupo
and p.codcompañia ='35' and a.codseccion ='18'
and (CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) >='20090101')
and (CAST(FLOOR(CAST(a.fecAtencion AS float)) AS datetime) <='20091231')
and h.modo ='Urgente'
and datename(weekday,a.fecatencion)!= 'Sabado'
and datename(weekday,a.fecatencion)!= 'Domingo'
AND NOT EXISTS (select * from diasfestivos af …Run Code Online (Sandbox Code Playgroud) 我试图使用TSQL子字符串函数获取Guid字段的第一部分,如下所示
SELECT SUBSTRING(Guid, 1, 8) AS Gu FROM MyTable
Run Code Online (Sandbox Code Playgroud)
但我得到的只是这个错误.
参数数据类型uniqueidentifier对子字符串函数的参数1无效.
那么这里发生了什么?我应该首先将Guid视为纯字符串还是......?
提前致谢!
我想创建一个存储过程,它接受一串逗号分隔值,如"1,2,3,4",并将其拆分并使用这些数字在不同的表上运行查询.
所以在相同的存储过程中它会做类似的事情
select somefield from sometable where somefield = 1
select somefield from sometable where somefield = 2
select somefield from sometable where somefield = 3
select somefield from sometable where somefield = 4
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有2个表,想要查询与艺术家联系的所有专辑数据
artists: artist_id | name
albums: album_id | artist_id | songs
这只给了我第一行
SELECT * FROM albums JOIN artists ON artists.artist_id = albums.artist_id
Run Code Online (Sandbox Code Playgroud)
我想指定一个专辑ID
SELECT * FROM albums
WHERE album_id = 101
JOIN artists ON artists.artist_id = albums.artist_id
Run Code Online (Sandbox Code Playgroud) 我有两个表emptable1(empid,status)emptable2(empid,week)
我想在emptable1中选择状态为0的所有empid,并且从empid列表中我需要从表emptable2中选择empid,其周为7
请帮忙 :-)