Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 use "variable" default value of field?

Author  Topic 

M_N_M
Starting Member

21 Posts

Posted - 2010-09-23 : 14:09:42
Hello,

is it possible to use a "variable" default value on a field.
I'll try to make it clear with an example.

I have a table "x" with the following fields:
company_id int
field1 char(20)
field2 varchar(100)

From within my application, I'm doing the following insert query:
INSERT INTO x (field1, field2) VALUES (?value1, ?value2)

I want the field "company_id" to be filled from a value that comes from a temporary table (company_id is either 1 or 2, based on the user that is logged in...)

I know you can use "functions" as a default value of a field BUT in functions you can't use temporary tables :-(
Is there a workaround? (is there no such thing as "user global variables" like @@ROWCOUNT ?)

If no, I need to use "INSTEAD OF" triggers but I don't like them and I try to avoid them as much as possible...


Thanks in advance

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-23 : 14:17:42
INSERT INTO x (company_id, field1, field2)
SELECT company_id, @field1, @field2
FROM #Temp
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

M_N_M
Starting Member

21 Posts

Posted - 2010-09-23 : 15:09:51
it's not that easy. This is an example for one query but I need to do it for the whole application (> 1.000.000 queries)
That's the reason why I want to use a default based on a temp table..
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-23 : 15:23:44
You'll need to provide a better description and example then.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -