需要检查一个临时表 [英] need to check for a temporary table

查看:49
本文介绍了需要检查一个临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想使用C#在我的应用程序中创建一个临时表.
首先,我需要检查一个临时表,然后创建一个不存在的表.

你能帮我吗?

在此先感谢.

Hi all,

I would like to create a temporary table in my application using C#.
First I need to check for a temporary table and then create the table if does not exist.

Can u help me?

Thanks in advance.

推荐答案

临时表带有#前缀,对于全局临时表则带有##,并且可以通过objectid访问.

更新存储过程中的临时表 [ ^ ]
Temp tables are prefixed with #, or ## for global temp tables, and are accessible by objectid.

Updating a temp table in a Stored Proc[^]


CREATE PROCEDURE MakeMyTempTable
AS
BEGIN
IF (SELECT 1 FROM #tempTable /*you can call your table however you want but for a temporary table #*/) IS NULL 
CREATE TABLE #tempTable 
( id int
  --here you define your fields
)
END



现在,要创建临时表,请从c#或sql中调用该过程.



Now, for create temp table call the procedure from c# or sql.


下面是存储过程
检查表是否在TempDB中已经存在.
(在SQLServer的TempDB中创建临时表.)
放下桌子
并创建一个表格
USE [sample1]
GO

设置ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
创建proc [dbo].[temp]


开始
如果存在
(
SELECT * FROM tempdb.dbo.sysobjects其中ID = OBJECT_ID(N''tempdb ..#Mytemp'')

)

删除表#Mytemp
ELSE

创建表#Mytemp(Col1 nvarchar(100),Col2 int)
结束
below is the store procedure
which check if table already exists in TempDB.
(Temporary tables are getting created in TempDB in SQLServer.)
Drop that table
and will create a table
USE [sample1]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[temp]
as

Begin
IF EXISTS
(
SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N''tempdb..#Mytemp'')

)

DROP TABLE #Mytemp
ELSE

CREATE TABLE #Mytemp(Col1 nvarchar(100), Col2 int)
End


这篇关于需要检查一个临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆