Generate table records for SQL statements

Source: Internet
Author: User


/*-------------------------------------------------------------------------------
Function:
Generate table record SQL statement (note text and ntext fields are exported to null)

Parameter description:
@Table_Name Table Name
@IsPrint whether to print input [1: Yes, 0: no].
Yes: The print string is used in Query Analyzer.
No: Select out table (default = 0: NO)

-When there is a self-increment [identity] in the table, place the resulting SQL statement in the Insert SQL statement below and execute
SET IDENTITY_INSERT [Thetablename] On
Go
.. Insert SQL statement
Go
SET IDENTITY_INSERT [Thetablename] Off
Go
-------------------------------------------------------------------------------------*/
DECLARE @Table_Name varchar ($), @IsPrint bit
Set @Table_Name = ' Erp_sys_menus '
Set @IsPrint = 1

SET NOCOUNT on
DECLARE @obj_name as SYSNAME
DECLARE @column_name as SYSNAME
DECLARE @usr_defined_dtype as SYSNAME
DECLARE @sys_dtype as SYSNAME

DECLARE @str_insert as VARCHAR (MAX)
DECLARE @str_value as VARCHAR (MAX)

DECLARE @cu_obj CURSOR
SET @cu_obj = CURSOR LOCAL SCROLL for
SELECT Sobj.name as Obj_name,
Scol.name as column_name,
Styp.name as Usr_defined_dtype,
Styp1.name as Sys_dtype
From sysobjects sobj
INNER JOIN syscolumns scol on scol.id = sobj.id
INNER JOIN systypes styp on styp.xtype = Scol.xtype and Styp.xusertype = Scol.xusertype
INNER JOIN systypes styp1 on styp1.xtype = Styp.xtype and Styp1.xusertype = Styp.xtype
WHERE sobj.xtype = ' U '
and sobj.name = @Table_Name
ORDER by Scol.colid

SET @str_insert = ' INSERT into [' + @table_name + '] ('
SET @str_value = "values (' + ')

OPEN @cu_obj
FETCH NEXT from @cu_obj into @obj_name, @column_name, @usr_defined_dtype, @sys_dtype

While @ @FETCH_STATUS = 0
BEGIN
IF @sys_dtype <> ' image '
BEGIN
SET @str_insert = @str_insert + ' [' + @column_name + '], '
BEGIN
SET @str_value = @str_value + ' case ' + @column_name + ' was null then ' null ' ' Else '
IF @sys_dtype in (' char ', ' varchar ', ' nchar ', ' nvarchar ')
BEGIN
SET @str_value = @str_value + "" "+" + "replace (' + @column_name + ', '" ' "') ' + '" ' "" "" "" "" "" "" "."
END
ELSE IF @sys_dtype in (' Text ', ' ntext ')
BEGIN
SET @str_value = @str_value + ' null '
END
ELSE IF @sys_dtype in (' DateTime ', ' datetime2 ', ' smalldatetime ', ' Date ')
BEGIN
SET @str_value = @str_value + "" + "+" convert (varchar, ' + @column_name + ', 120) ' + ' + ' "
END
ELSE IF @sys_dtype in (' bigint ', ' int. ', ' smallint ', ' tinyint ', ' bit ', ' decimal ', ' numeric ', ' money ', ' smallmoney ', ' float ' , ' real ')
BEGIN
SET @str_value = @str_value + ' convert (varchar, ' + @column_name + ') ' + '
END
ELSE
BEGIN
SET @str_value = @str_value + "" "+" + "+ @column_name + ' +" "
END
SET @str_value = @str_value + ' End '
END
SET @str_value = @str_value + ' + ', ' + '
END
FETCH NEXT from @cu_obj into @obj_name, @column_name, @usr_defined_dtype, @sys_dtype
END
CLOSE @cu_obj

SELECT @str_insert = Left (@str_insert, LEN (@str_insert)-1) + ') '
SELECT @str_value = Left (@str_value, LEN (@str_value)-8) + ' + ') '

CREATE TABLE #returnTable (sqlString VARCHAR (MAX))
DECLARE @sql VARCHAR (MAX)
INSERT into #returnTable
EXEC (' select ' + @str_insert + ' + char (+) + ' + @str_value + ' + char (Ten) from ' + @table_name + ')
IF @IsPrint = 0
SELECT * from #returnTable
ELSE
BEGIN
DECLARE @PrintString as VARCHAR (MAX)
DECLARE print_cursor Cursor FOR
SELECT sqlString
From #returnTable

OPEN Print_cursor

FETCH NEXT from Print_cursor to @PrintString
While @ @FETCH_STATUS = 0
BEGIN
PRINT @PrintString
FETCH NEXT from Print_cursor to @PrintString
END

CLOSE Print_cursor
Deallocate print_cursor
END
DROP TABLE #returnTable
SET NOCOUNT OFF

Generate table records for SQL statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.