Insert command and variables : Insert Data « Transact SQL « SQL Server / T-SQL






Insert command and variables


25>
26> IF EXISTS(SELECT name FROM sys.tables
27>     WHERE name = 'T')
28>     DROP TABLE T
29> GO
1>
2> CREATE TABLE T (
3>     c1 int,
4>     c2 varchar(8000)
5> )
6> GO
1>
2> DECLARE @v1 varchar(max)
3>
4> SET @v1 = REPLICATE('A',7999) + 'B'
5> INSERT T VALUES (1, @v1)
6> SELECT RIGHT(c2,2) 'Right 2 of c2' FROM T
7>
8> SET @v1 = @v1 + 'B'
9> INSERT T VALUES (2, @v1)
10> SELECT RIGHT(c2,2) 'Right 2 of c2' FROM T
11> GO

(1 rows affected)
Right 2 of c2
-------------
AB
Msg 8152, Level 16, State 10, Server JAVA2S\SQLEXPRESS, Line 9
String or binary data would be truncated.
The statement has been terminated.

(1 rows affected)
Right 2 of c2
-------------
AB

(1 rows affected)
1>
2> drop table t
3> GO
           
       








Related examples in the same category

1.Insert data to a table in the procedure
2.Checks for existing Product record: update or insert
3.Insert all statements in a batch or to roll back the entire statement group if an error occurs
4.Stored procedure: accepts a ID, name and city and inserts them as new row
5.Managing Inserts Using Stored Procedures