---
title: "Stored prodedure"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/45241-stored-prodedure"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "erka"
published: "2002-06-05T11:55:48.000Z"
updated: "2002-06-05T13:01:12.000Z"
replies: 7
views: 232
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/45241-stored-prodedure"
---

# Stored prodedure

## #1 — erka, 2002-06-05T11:55Z

Hej, i databasen jag har kan man ha två värden (olika grupper som ska visas), de är antingen 1 eller 2. Dessa värden skickar man med till sp:n för att få ut en grupp, nu försöker jag fixa så att om man skickar med 3 ska man få med båda grupperna.

Nu har jag följande kod

```
@visibility int,
@language int,
@group varchar(100)

AS

IF @group = 3
BEGIN
SET @group = '1 OR Model.FKgroupID = 2'
END

SELECT     Model.PKmodelID, Model.ModelName, ModelDescription.FullModelName, ModelDescription.Description
FROM         Model INNER JOIN
                      ModelDescription ON Model.PKmodelID = ModelDescription.PKmodelID INNER JOIN
                      ModelVisibilityLink ON Model.PKmodelID = ModelVisibilityLink.PKmodelID INNER JOIN
                      ProductGroup ON Model.FKproductGroupID = ProductGroup.PKproductGroupID
WHERE ModelDescription.PKlanguageID = @language AND ModelVisibilityLink.PKvisibilityID = @visibility AND Model.FKgroupID = @group
```

Om jag skickar med 3 får jag följande felmeddelande

Syntax error converting the varchar value '1 OR Model.FKgroupID = 2' to a column of data type int.

(det funkar med att skicka med 1 eller 2 dock, vilket det ska), så min fråga är hur fan jag konverterar det rätt så att säga.

Tacksam

Permalänk: https://www.webforum.nu/p/45241

## #2 — @nders, 2002-06-05T12:02Z

Kan man inte göra så här?

```
@visibility int,
@language int,
@group varchar(100)

AS

IF @group = 3
BEGIN
SET @group = '1, 2'
END

SELECT     Model.PKmodelID, Model.ModelName, ModelDescription.FullModelName, ModelDescription.Description
FROM         Model INNER JOIN
                      ModelDescription ON Model.PKmodelID = ModelDescription.PKmodelID INNER JOIN
                      ModelVisibilityLink ON Model.PKmodelID = ModelVisibilityLink.PKmodelID INNER JOIN
                      ProductGroup ON Model.FKproductGroupID = ProductGroup.PKproductGroupID
WHERE ModelDescription.PKlanguageID = @language AND ModelVisibilityLink.PKvisibilityID = @visibility AND Model.FKgroupID IN (@group)
```

Det kanske inte fungerar dock. Annars får du lägga in alltihop i en sträng som du sedan exekverar med EXEC.

Mvh,

Permalänk: https://www.webforum.nu/p/689992

## #3 — erka, 2002-06-05T12:05Z

Nej får samma felmeddelande fast på 1, 2 då. Det var just det jag undrade hur man sätter ihop allt till en sträng som jag exekverar, alltså bygga upp strängen med lite till om det skulle vara en 3:a i @group

tack

Permalänk: https://www.webforum.nu/p/689995

## #4 — @nders, 2002-06-05T12:08Z

Exempel:

```
declare @query varchar(8000)
...
SET @group = '1, 2'
SET @query = 'SELECT * FROM tabellen WHERE fältet IN (' + @group + ')'

EXEC @query
```

Mvh,

Permalänk: https://www.webforum.nu/p/690001

## #5 — LarsG, 2002-06-05T12:26Z

```
declare @g1, @g2 int
if @group = 1 select @g1 = 1,@g2 = 1
if @group = 2 select @g1 = 2,@g2 = 2
if @group = 3 select @g1 = 1,@g2 = 2 

SELECT     Model.PKmodelID, Model.ModelName, ModelDescription.FullModelName, ModelDescription.Description
FROM         Model INNER JOIN
                      ModelDescription ON Model.PKmodelID = ModelDescription.PKmodelID INNER JOIN
                      ModelVisibilityLink ON Model.PKmodelID = ModelVisibilityLink.PKmodelID INNER JOIN
                      ProductGroup ON Model.FKproductGroupID = ProductGroup.PKproductGroupID
WHERE ModelDescription.PKlanguageID = @language AND ModelVisibilityLink.PKvisibilityID = @visibility AND Model.FKgroupID IN (@g1,@g2)
```

så slipper du dynamisk sql 

exec (@query)

Permalänk: https://www.webforum.nu/p/690011

## #6 — erka, 2002-06-05T12:32Z

Tack båda, men jag förstår inte riktigt hur jag ska skriva den i spn

koden nu blir

```
CREATE PROCEDURE [spModels]
@visibility int,
@language int,
@group int,
@g1 int,
@g2 int
AS

if @group = 1 select @g1 = 1,@g2 = 1
if @group = 2 select @g1 = 2,@g2 = 2
if @group = 3 select @g1 = 1,@g2 = 2 

SELECT     Model.PKmodelID, Model.ModelName, ModelDescription.FullModelName, ModelDescription.Description
FROM         Model INNER JOIN
                      ModelDescription ON Model.PKmodelID = ModelDescription.PKmodelID INNER JOIN
                      ModelVisibilityLink ON Model.PKmodelID = ModelVisibilityLink.PKmodelID INNER JOIN
                      ProductGroup ON Model.FKproductGroupID = ProductGroup.PKproductGroupID
WHERE ModelDescription.PKlanguageID = @language AND ModelVisibilityLink.PKvisibilityID = @visibility AND Model.FKgroupID IN (@g1,@g2)
GO
```

Permalänk: https://www.webforum.nu/p/690018

## #7 — LarsG, 2002-06-05T12:55Z

```
CREATE PROCEDURE spModels
@visibility int,
@language int,
@group int 
AS
declare @g1 int,@g2 int
if @group = 1 select @g1 = 1,@g2 = 1
if @group = 2 select @g1 = 2,@g2 = 2
if @group = 3 select @g1 = 1,@g2 = 2 

SELECT     Model.PKmodelID, Model.ModelName, ModelDescription.FullModelName, ModelDescription.Description
FROM         Model INNER JOIN
                      ModelDescription ON Model.PKmodelID = ModelDescription.PKmodelID INNER JOIN
                      ModelVisibilityLink ON Model.PKmodelID = ModelVisibilityLink.PKmodelID INNER JOIN
                      ProductGroup ON Model.FKproductGroupID = ProductGroup.PKproductGroupID
WHERE ModelDescription.PKlanguageID = @language AND ModelVisibilityLink.PKvisibilityID = @visibility AND Model.FKgroupID IN (@g1,@g2)
GO
```

Permalänk: https://www.webforum.nu/p/690026

## #8 — erka, 2002-06-05T13:01Z

LarsG, stort tack, nu ser jag ju hur man kan göra. Lärde mig mycket på det, danke danke. @nders, du ska ha tack oxå :)

Permalänk: https://www.webforum.nu/p/690030

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/45241-stored-prodedure
