USE [your_DB];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
ALTER PROCEDURE proc_CreateInsertForTable
@tableName NVARCHAR(1500) --2,000, 2,500 เต็มที่ไม่เกิน 3 พัน (THB)
AS
/***************************************************************************
Written by Jennifer McCown, 9/13/2009
[email protected]
http://www.MidnightDBA.com
Not copywrited; use, modify, and distribute freely!
Description: Generate an insert statement for a given table.
@withDescription controls the appearence of the "VALUES" portion of the
generated input statement.
Please note that the insert statement will not include Identity columns.
EXEC exec proc_CreateInsertForTable @tableName = 'Video', @withDescription = 1
Last Modify By : หน้าฮี 9/14/2009
****************************************************************************/
SET NOCOUNT ON;
DECLARE @sql1 VARCHAR(MAX), @sql2 VARCHAR(MAX), @name VARCHAR(100), @type VARCHAR(100);
DECLARE @mytable TABLE
(
column_id INT, name NVARCHAR(500), system_type_id INT, max_length INT, precision INT, scale INT, is_nullable BIT, is_identity BIT, readableType VARCHAR(100)
);
INSERT INTO @myTable
SELECT column_id, c.name, c.system_type_id, c.max_length, c.precision, c.scale, c.is_nullable, c.is_identity, t.name readableType
FROM sys.columns c
JOIN sys.types t ON c.system_type_id = t.system_type_id
WHERE OBJECT_NAME(c.object_id) = @tableName
ORDER BY c.column_id;
-- Update ReadableType columns
UPDATE @myTable
SET readableType = readableType+' ('+CAST(max_length AS NVARCHAR(10)
)+')'
WHERE readableType IN
('char', 'varchar', 'nchar', 'nvarchar'
);
UPDATE @myTable
SET readableType = readableType+' ('+CAST(precision AS NVARCHAR(10)
)+', '+CAST(scale AS NVARCHAR(10)
)+')'
WHERE readableType IN
('decimal', 'numeric'
);
UPDATE @myTable
SET readableType = readableType+' ('+CAST(precision AS NVARCHAR(10)
)+')'
WHERE readableType = 'float';
-- Seed the SQL variables with INSERT syntax
SET @sql1 = 'INSERT INTO '+@tableName+' (';
SET @sql2 = ') '+'VALUES (';
-- Open a cursor to add all the columns to INSERT statement
DECLARE curInsert CURSOR
FOR SELECT name, readableType
FROM @myTable
WHERE is_identity = 0;
OPEN curInsert;
-- Get first column
FETCH NEXT FROM curInsert INTO @name, @type;
SET @sql1 = @sql1 + @name; --INSERT INTO PL_EmpPicture (EmpID
SET @sql2 = @sql2+'@'+@name;
-- Add additional columns
FETCH NEXT FROM curInsert INTO @name, @type;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql1 = @sql1+'ฮี'+', '+@name;
SET @sql2 = @sql2+'ฮี'+', @'+@name;
FETCH NEXT FROM curInsert INTO @name, @type;
END;
CLOSE curInsert;
DEALLOCATE curInsert;
SET @sql2 = @sql2+')';
SELECT @sql1, @sql2;
PRINT @sql1 + @sql2;
SET NOCOUNT OFF;
RETURN 0;
ปล. สิ่งที่อยากได้เพิ่มเติม
--- ไม่อยากจะใช้ Cursor แต่อยากจะใช้ Table Value
--- อยากได้แบบที่ Generate Class พร้อมใช้งานบน .NET (เช่น EF/NHI/etc..)
DECLARE @tmp_TableA TABLE (gหอย varchar(10), gเหอ varchar(20), gPrice int)
INSERT INTO @tmp_TableA
SELECT 'ห1', 'หอย1', 1 UNION ALL
SELECT 'ห2', 'หอย2', 1 UNION ALL
SELECT 'ห3', 'หอย3', 2 UNION ALL
SELECT 'ห4', 'หอย4', 2 UNION ALL
SELECT 'ห5', 'หอย5', 3
--SELECT * FROM @tmp_TableA
DECLARE @intCount INT = 1
DECLARE @rowCount int = (SELECT COUNT('หอย') FROM @tmp_TableA)
--SET @intCount = 1
--WHILE @intCount <= @rowCount
WHILE (@intCount <= 10) BEGIN
PRINT @intCount
--CONTINUE;
IF @intCount = 4 -- This will never executed (Seed)
BEGIN
PRINT @intCount
PRINT 'Break'
BREAK;
END
ELSE BEGIN
DECLARE @tmpVar int = 0
SET @tmpVar = @tmpVar + 1
END
SELECT * FROM @tmp_TableA WHERE gPrice = @intCount
SET @intCount = @intCount + 1
CONTINUE
END
GO
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Public Class FullModelSampleWithoutEF
''' <summary>
''' ชื่อเมียน้อย
''' </summary>
<Required(ErrorMessage:="FirstName {0} is required")> _
<StringLength(100, MinimumLength:=1, ErrorMessage:="Name Should be minimum 1 characters and a maximum of 100 characters")> _
<DataType(DataType.Text)> _
Public Property FirstName As String
<RegularExpression("([0-9]+)", ErrorMessage:="Please enter valid Number")> _
Public Property LastName As String
<Range(1, 13, ErrorMessage:="อายุเมียน้อยต้องอยู่ระหว่าง 0 - 13 ปี (ข้อหาพรากกระชากผู้สูงวัย)")> _
Public Property Age As Integer
End Class
Validating Code (VB.NET)
Dim obj As New FullModelSampleWithoutEF
obj.FirstName = "ABC"
obj.LastName = "A1234" 'Force Error
obj.Age = "14" 'Force Error
'Fake Validator
Dim context As System.ComponentModel.DataAnnotations.ValidationContext = New System.ComponentModel.DataAnnotations.ValidationContext(obj, Nothing, Nothing)
Dim result As List(Of System.ComponentModel.DataAnnotations.ValidationResult) = New List(Of System.ComponentModel.DataAnnotations.ValidationResult)()
Call System.ComponentModel.DataAnnotations.Validator.TryValidateObject(obj, context, result, True)
If result.Count > 0 Then
MessageBox.Show(result(0).MemberNames(0) & " : " & result(0).ErrorMessage)
Return
End If