Skip to content

Commit e9fa154

Browse files
rrg92rwestMSFT
authored andcommitted
Add impact on sp_executesql and EXEC statement
Add more examples and notes about impact of this settings in some common commands and Jobs
1 parent a358cd9 commit e9fa154

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/t-sql/statements/set-textsize-transact-sql.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,40 @@ SET TEXTSIZE { number }
5050
Setting SET TEXTSIZE affects the @@TEXTSIZE function.
5151

5252
The setting of set TEXTSIZE is set at execute or run time and not at parse time.
53+
54+
### Impacts on `sp_executesql` and `EXECUTE` Statements
55+
The `TEXTSIZE` setting affects the result of dynamic statements executed by [sp_executesql (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-executesql-transact-sql.md) or [EXECUTE (Transact-SQL)](../../t-sql/language-elements/execute-transact-sql.md), especially when the result is inserted into a table.
56+
57+
Consider the following example:
58+
59+
```sql
60+
SET TEXTSIZE -1;
61+
62+
DROP TABLE IF EXISTS #TestTextSize;
63+
CREATE TABLE #TestTextSize(txt varchar(5))
64+
65+
SET TEXTSIZE 2;
66+
67+
INSERT INTO #TestTextSize
68+
SELECT CONVERT(text,'12345')
69+
70+
INSERT INTO #TestTextSize
71+
EXEC('SELECT CONVERT(text,''12345'')')
72+
73+
SELECT * FROM #TestTextSize
74+
```
75+
76+
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
77+
78+
```
79+
txt
80+
-------
81+
12345
82+
12
83+
```
84+
85+
Note that regardless of the target table's correct data type, SQL truncates the text before insertion due to the `TEXTSIZE` setting. When using SQL Server Agent Jobs that runs `EXEC` or `sp_executesql`, you must be cautious. SQL Server Agent modifies the default value of `TEXTSIZE`, which could lead to unexpected behavior. For example, queries may work fine in SQL Server Management Studio but fail when executed through SQL Agent, especially when working with certain text columns that exceed the size. To prevent this issue, ensure you add `SET TEXTSIZE` to the query that the job runs, explicitly defining the desired text size to avoid truncation
86+
5387

5488
## Permissions
5589
Requires membership in the **public** role.

0 commit comments

Comments
 (0)