如何参数化sql查询

时间:2014-04-14 16:38:32

标签: jquery mysql sql database

如何参数化简单的create table sql查询?

CREATE TABLE `rmr_form` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(80) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `ip` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
)

请帮忙解释一下。我真的很感激。 :( 谢谢你们!

1 个答案:

答案 0 :(得分:0)

你可以使用proc。

来完成
CREATE PROCEDURE [dbo].[sp_tabCreate]
        @tabName        nvarchar(30)
        AS
        BEGIN

        IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@tabName) AND type in (N'U'))
DROP TABLE [dbo].[@tabName]

DECLARE @SQLString NVARCHAR(MAX)
    Set @SQLString = 'CREATE TABLE ' +@tabName+
    '(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [RankID] [int] NOT NULL,
    [SlotTime] [nvarchar](10) NOT NULL,
    [SlotDate] [nvarchar](30) NOT NULL
) ON [PRIMARY]'

EXEC (@SQLString)
END

并将字段切换为params,如' @ tabName'