Question: How to insert a sql script in a table column?

I have a table column which has ntext datatype. I will have to insert the whole function or stored procedure in the column.

Giving an example : sp\_helptext 'sp\_TestProcedure' will return the complete syntax of a stored procedure. How to populate the stored procedure script in a Table.

I can change the data type either ntext or nvarchar(max). Actual question is , how to insert the script in a column ?

This is not an insert of stored procedure result. This script which i am looking for is to insert the actual stored procedure (or) function (or) view in a table

Answer: 
You could take a look at sys.sql\_modules, which contains definitions (code) for database objects.

```
INSERT INTO [dbo].[some_table] ([schema_name], [object_name], [definition])
SELECT 
    OBJECT_SCHEMA_NAME([object_id]) [schema_name],
    OBJECT_NAME([object_id]) [object_name],
    [definition]
FROM sys.sql_modules
WHERE   OBJECT_SCHEMA_NAME([object_id]) = 'dbo'
    AND OBJECT_NAME([object_id]) = 'some_object'

```

**Update:** As others have commented, if the purpose is to maintain version history it may be more effective to use some other source code control solution. Also, if you want to track any time code in database objects change you could look into implementing a DDL trigger. Just searching "ddl trigger to track schema changes" produced some promising results.

Also, I just stumbled across OBJECT\_DEFINITION(), which may be helpful:

```
SELECT OBJECT_DEFINITION(OBJECT_ID('dbo.spt_values'))

```
script is text. simply use the regular 'insert into' that sql server has.

when that script is inside a file, you need to read the contents of that file first. the method of doing that depends on the type of language you use - c, c#, java or python (whichever).

if you want to retrieve it, use the normal 'select' command.

however, I do not believe it's a good way of storing functions. being inside a file-system works (usually).