以编程方式在sql中创建表 [英] create table in sql programatically

查看:78
本文介绍了以编程方式在sql中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下定义的表

column1 int

column2 nvarchar(50)

i知道表模式但我想要在数据库中创建这个表,在执行之前知道它的名字是什么?b $ b所以我需要一个动态脚本在未知数据库中创建一个表

你可以帮助我吗?



我想在sql server脚本中找到解决方案,我想要它动态

i have a table that has the following definition
column1 int
column2 nvarchar(50)
i know the table schema but i want to create this table in a database that idon't
know its name until execution
so i need a dynamic script to create a table in unknown database
can you help me?

iwant the solution in script in sql server and i want it dynamic

推荐答案

假设SQL服务器,这里有T-SQL语法: http://msdn.microsoft .com / zh-CN / library / ms174979.aspx [ ^ ]

假设你想从c#中做到这一点,这里有一些你可以使用的例子: HT tp://www.c-sharpcorner.com/UploadFile/mahesh/CreatingDBProgrammaticallyMCB11282005064852AM/CreatingDBProgrammaticallyMCB.aspx [ ^ ]



顺便问一下,你怎么样?我们可以猜测你打算使用哪种RDBMS和语言?
Supposing SQL server, here you have the T-SQL syntax: http://msdn.microsoft.com/en-us/library/ms174979.aspx[^]
Supposing you want to do it from c#, here you have some examples you can use: http://www.c-sharpcorner.com/UploadFile/mahesh/CreatingDBProgrammaticallyMCB11282005064852AM/CreatingDBProgrammaticallyMCB.aspx[^]

By the way, how do you think we can guess which RDBMS and language you intend to use?


Create Procedure [dbo].[PrepareDataBase]
@DataBaseName sysname
as

--create table Relations Definition in specific database
Declare @createSql nvarchar(2000)
set @createSql='use' + QUOTENAME(@DataBaseName)

Set  @createSql = @createSql + ' Create Table Relations
(
	[Foreign_Table] [nvarchar](255) NULL,
	[Foreign_Key] [nvarchar](255) NULL,
	[Primary_Table] [nvarchar](255) NULL,
	[Primary_Key] [nvarchar](255) NULL
) ON [PRIMARY]
'
Exec (@createSql)





感谢每一个人帮助我



thanks for every one helped me


这篇关于以编程方式在sql中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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