T-SQL question | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

T-SQL question

I’m quite new at T-SQL, and am having some problems. I hope it’s ok to post here asking for help – if not – just ignore this post. [<img src=’/community/emoticons/emotion-2.gif’ alt=’:D‘ />]<br /><br />I want to insert a variable number of records into my table when I call a procedure – However, everytime I call it, all the data in all the fields of the table is going to be the exact same EXCEPT the field that I pull with a SQL statement.<br /><br />Essentially what I want to do is this:<br /><br /><br /><pre id="code"><font face="courier" size="2" id="code"><br />–variables passed to stored procedure<br />@FIFO smalldatetime, –fifo calculation of request date/time<br />@SEID char(5), –employee ID<br /><br /><br />INSERT INTO VACATION_REQUESTS (e_seid, fifo_date, day_off)<br />VALUES (@SEID, @FIFO, (SELECT * FROM TABLE))<br /><br /></font id="code"></pre id="code"><br /><br />Spent hours researching, and it sounds like I might need a Cursor to pull this off? Advice appreciated.
1. Re-write as: INSERT INTO VACATION_REQUESTS (e_seid, fifo_date, day_off)
SELECT @SEID, @FIFO, * FROM TABLE

OR 2. I have a feeling the other data is dependant on the employee ID passed into the procedure. So include a WHERE clause in the SELECT.. INSERT INTO VACATION_REQUESTS (e_seid, fifo_date, day_off)
SELECT @SEID, @FIFO, * FROM TABLE WHERE…..

I would remove the star (*) and use the actual column name if I were in your situation. You are only getting one column back from the other table. Nathan H. Omukwenyi
MVP [SQL Server]

Fantastic, thanks for quick response – I’ll give it a go tomorrow. That was just a simple SELECT to demonstrate my quandry, the actual query I will be using is below. Assuming I can figure out how to make MINUS work in TSQL. –other variable not mentioned in other post
@sText varchar(8000) –delimted text of vacation dates
— split the requested vacation days
SELECT CAST(value AS SMALLDATETIME)
FROM dbo.fn_Split(@sText, ‘,’)
— subtract out any that already exist
MINUS
SELECT [day_off]
FROM dbo.VACATION_REQUESTS
WHERE @seid = e_seid
If your using SQL Server 2005 , you would replace ‘MINUS’ with ‘EXCEPT’. With SQL Server 2000 you can use the ‘NOT IN’ operator to prune the results: SELECT CAST(value AS SMALLDATETIME) FROM dbo.fn_Split(@sText, ‘,’)
WHERE CAST(value AS SMALLDATETIME) NOT IN
(SELECT [day_off]FROM dbo.VACATION_REQUESTS WHERE @seid = e_seid) Nathan H. Omukwenyi
MVP [SQL Server]

Works perfectly. Thanks so much vbkenya. Really appreciate it – you saved me hours of googling and experimenting.
Also refer this
http://vyaskn.tripod.com/oracle_sql_server_differences_equivalents.htm Madhivanan Failing to plan is Planning to fail
If your using SQL Server 2005 , you would replace ‘MINUS’ with ‘EXCEPT’. With SQL Server 2000 you can use the ‘NOT IN’ operator to prune the results:
———————————————–
yes
]]>