如何在SQL Server中执行多个查询?

时间:2022-09-01 20:42:58

I am trying to insert an account info for example cash in table TBL_Account and at the same time I want to create a table with the same account name Cash.

我正在尝试在表TBL_Account中插入一个帐户信息,例如现金,同时我想创建一个具有相同帐户名称Cash的表。

ALTER PROCEDURE [dbo].[usp_AccountSaveAccount]
   @AccountId int,
   @AccountName nvarchar(Max),
   @PortalId int,
   @CultureCode nvarchar(100),
   @UserModuleId int,
   @AddedBy nvarchar(100)
AS
BEGIN
   DECLARE @SQLInsertString NVARCHAR(MAX)

   if(@AccountId = 0)
   BEGIN
      INSERT INTO dbo.AccountTable
    (
    AccountId,
    AccountName,
    PortalId,
    CultureCode,
    UserModuleId,
    AddedBy,
    AddedOn,
    UpdatedBy,
    UpdatedOn,
    IsDeleted
    )
    VALUES
    (
    @AccountId,
    @AccountName,
    @PortalId,
    @CultureCode,
    @UserModuleId,
    @AddedBy,
    GETDATE(),
    @AddedBy,
    GETDATE(),
    0
    );

    END

    DECLARE @SQLString NVARCHAR(MAX)

    SET @SQLString = 'CREATE TABLE [dbo].['+@AccountName+'](
    [AccountCatId] [int] IDENTITY(1,1) NOT NULL,
    [AccountCatName] [nvarchar](max) NULL,
    [InsertedDate] [datetime] NULL,
    [Particulars] [nvarchar](max) NULL,
    [ParticularsBy] [nvarchar](max) NULL,
    [ParticularsTo] [nvarchar](max) NULL,
    [Dr] [int] NULL,
    [Cr] [int] NULL,
    [DebitedAccountId] [int] NULL,
    [CreditedAccountId] [int] NULL,
    [DebitAmount] [float] NULL,
    [CreditAmount] [float] NULL,
    [AddedBy] [nvarchar](100) NULL,
    [AddedOn] [datetime] NULL,
    [UpdatedBy] [nvarchar](100) NULL,
    [UpdatedOn] [datetime] NULL,
    [IsDeleted] [bit] NULL,
    [DeletedBy] [nvarchar](100) NULL,
    [DeletedOn] [datetime] NULL,
 CONSTRAINT [PK_'+ @AccountName +'] PRIMARY KEY CLUSTERED 
(
    [AccountCatId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]'

EXEC (@SQLString)

    END

But it only creates table but does not insert into the table.

但它只创建表但不插入表中。

Any suggestion will be appreciated.

任何建议将不胜感激。

1 个解决方案

#1


1  

You can convert nvarchar to varchar in create query

您可以在create query中将nvarchar转换为varchar

like this

SET @SQLString = 'CREATE TABLE [dbo].['+convert(varchar(200),@AccountName)+'](

or

CONSTRAINT [PK_'+ convert(varchar(200),@AccountName) +'] PRIMARY KEY CLUSTERED 

#1


1  

You can convert nvarchar to varchar in create query

您可以在create query中将nvarchar转换为varchar

like this

SET @SQLString = 'CREATE TABLE [dbo].['+convert(varchar(200),@AccountName)+'](

or

CONSTRAINT [PK_'+ convert(varchar(200),@AccountName) +'] PRIMARY KEY CLUSTERED