Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This script generates insert statements of your existing data. This is a stored procedure which you need to run once and then it is tailor made for you.</p> <p>I tried to find this kind of stuff for a while but wasn't satisfied with the results, so I wrote this stored procedure.</p> <p>Example:</p> <pre><code>Exec [dbo].[INS] 'Dbo.test where 1=1' </code></pre> <p>(1) Here <code>dbo</code> is schema and test is tablename and <code>1=1</code> is condition. </p> <pre><code>Exec [dbo].[INS] 'Dbo.test where name =''neeraj''' * for string </code></pre> <p>(2) Here <code>dbo</code> is schema and test is tablename and <code>name='neeraj'</code> is condition.</p> <p>Here is the stored procedure</p> <pre><code>/* Authore : neeraj prasad sharma (please dont remove this :)) Example (1) Exec [dbo].[INS] 'Dbo.test where 1=1' (2) Exec [dbo].[INS] 'Dbo.test where name =''neeraj''' * for string here Dbo is schema and test is tablename and 1=1 is condition */ CREATE procedure [dbo].[INS] ( @Query Varchar(MAX) ) AS SET nocount ON DECLARE @WithStrINdex as INT DECLARE @WhereStrINdex as INT DECLARE @INDExtouse as INT DECLARE @SchemaAndTAble VArchar(270) DECLARE @Schema_name varchar(30) DECLARE @Table_name varchar(240) DECLARE @Condition Varchar(MAX) SET @WithStrINdex=0 SELECT @WithStrINdex=CHARINDEX('With',@Query ) , @WhereStrINdex=CHARINDEX('WHERE', @Query) IF(@WithStrINdex!=0) SELECT @INDExtouse=@WithStrINdex ELSE SELECT @INDExtouse=@WhereStrINdex SELECT @SchemaAndTAble=Left (@Query,@INDExtouse-1) SELECT @SchemaAndTAble=Ltrim (Rtrim( @SchemaAndTAble)) SELECT @Schema_name= Left (@SchemaAndTAble, CharIndex('.',@SchemaAndTAble )-1) , @Table_name = SUBSTRING( @SchemaAndTAble , CharIndex('.',@SchemaAndTAble )+1,LEN(@SchemaAndTAble) ) , @CONDITION=SUBSTRING(@Query,@WhereStrINdex+6,LEN(@Query))--27+6 DECLARE @COLUMNS table (Row_number SmallINT , Column_Name VArchar(Max) ) DECLARE @CONDITIONS as varchar(MAX) DECLARE @Total_Rows as SmallINT DECLARE @Counter as SmallINT DECLARE @ComaCol as varchar(max) SELECT @ComaCol='' SET @Counter=1 SET @CONDITIONS='' INSERT INTO @COLUMNS SELECT Row_number()Over (Order by ORDINAL_POSITION ) [Count], Column_Name FROM INformation_schema.columns WHERE Table_schema=@Schema_name AND table_name=@Table_name SELECT @Total_Rows= Count(1) FROM @COLUMNS SELECT @Table_name= '['+@Table_name+']' SELECT @Schema_name='['+@Schema_name+']' While (@Counter&lt;=@Total_Rows ) begin --PRINT @Counter SELECT @ComaCol= @ComaCol+'['+Column_Name+'],' FROM @COLUMNS WHERE [Row_number]=@Counter SELECT @CONDITIONS=@CONDITIONS+ ' + Case When ['+Column_Name+'] is null then ''Null'' Else '''''''' + Replace( Convert(varchar(Max),['+Column_Name+'] ) ,'''''''','''' ) +'''''''' end+'+''',''' FROM @COLUMNS WHERE [Row_number]=@Counter SET @Counter=@Counter+1 End SELECT @CONDITIONS=Right(@CONDITIONS,LEN(@CONDITIONS)-2) SELECT @CONDITIONS=LEFT(@CONDITIONS,LEN(@CONDITIONS)-4) SELECT @ComaCol= substring (@ComaCol,0, len(@ComaCol) ) SELECT @CONDITIONS= '''INSERT INTO '+@Schema_name+'.'+@Table_name+ '('+@ComaCol+')' +' Values( '+'''' + '+'+@CONDITIONS SELECT @CONDITIONS=@CONDITIONS+'+'+ ''')''' SELECT @CONDITIONS= 'Select '+@CONDITIONS +'FRom ' +@Schema_name+'.'+@Table_name+' With(NOLOCK) ' + ' Where '+@Condition print(@CONDITIONS) Exec(@CONDITIONS) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload