0

how can I insert to a table using values in different way? preferably no temp table. below is my stored procedure code, but i get errors on insert

CREATE PROCEDURE setBARS -- Add the parameters for the stored procedure here @BUSINESSAREANAME nvarchar(50), @STAFFNAME nvarchar(50), @ROLENAME nvarchar(50), @BARSSTARTDATE date, @BARSENDDATE date AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here WITH t1 (BUSINESSAREAID) AS (SELECT BUSINESSAREAID FROM BUSINESSAREA WHERE BUSINESSAREANAME = @BUSINESSAREANAME), t2 (STAFFID) AS (SELECT STAFFID FROM STAFF WHERE STAFFNAME = @STAFFNAME), t3 (ROLEID) AS (SELECT ROLEID FROM ROLE WHERE ROLENAME = @ROLENAME) INSERT INTO BARS ([BUSINESSAREAID],[STAFFID],[ROLEID],[BARSSTARTDATE],[BARSENDDATE]) VALUES ((SELECT t1.BUSINESSAREAID, t2.STAFFID, t3.ROLEID FROM t1,t2,t3), @BARSSTARTDATE, @BARSENDDATE) END GO 
2
  • 1
    insert into t1 (c1, c2...) select cx, literal... from ... Commented Jan 25, 2018 at 13:29
  • Please add the error(s). Commented Jan 25, 2018 at 13:55

3 Answers 3

1

You should be able to include the values directly in the SELECT statement:

INSERT INTO [BARS] ( [BUSINESSAREAID] , [STAFFID] , [ROLEID] , [BARSSTARTDATE] , [BARSENDDATE] ) SELECT [t1].[BUSINESSAREAID] , [t2].[STAFFID] , [t3].[ROLEID] , @BARSSTARTDATE , @BARSENDDATE FROM [t1] , [t2] , [t3]; 
Sign up to request clarification or add additional context in comments.

Comments

0

Try This

insert into bars([BUSINESSAREAID],[STAFFID],[ROLEID],[BARSSTARTDATE][BARSENDDATE]) SELECT t1.BUSINESSAREAID, t2.STAFFID, t3.ROLEID FROM t1,t2,t3, @BARSSTARTDATE, @BARSENDDATE 

1 Comment

no it does not work. the error :the select list for the INSERT statement contains fewer items than the insert list.
0

why not put all the values on a variable for example;

declare @val1 set @val1 = (select top 1 businessareaid from t1)

declare @val2 set @val1 = (select top 1 staffid from t2)

insert into bars (val1,val2) select @val1 , @val2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.