1

Topic: Dynamic change of type

The people, I want to change "on the fly" type change
SELECT @maxLenght = MAX (Len (RTRIM (text))) from Branches
select CAST (ISNULL (lstInvent.text, ' Start: ' +p.text) as VARCHAR (@maxLenght)) AS text,
ISNULL (lstInvent.data, @xStDatetIme) as Data,
p.id AS LocCode
from branches p
SQL swears
Line 4: Incorrect syntax near ' @maxLenght '.
What not so?

2

Re: Dynamic change of type

Valerii wrote:

the People, I want to change "on the fly" type change
SELECT @maxLenght = MAX (Len (RTRIM (text))) from Branches
select CAST (ISNULL (lstInvent.text, ' Start: ' +p.text) as VARCHAR (@maxLenght)) AS text,
ISNULL (lstInvent.data, @xStDatetIme) as Data,
p.id AS LocCode
from branches p
SQL swears
Line 4: Incorrect syntax near ' @maxLenght '.
What not so?

As it is declared
@maxLenght?
As with an error in a word or without?
@maxLenght? @maxLength?

3

Re: Dynamic change of type

Valerii;

wrote:

as VARCHAR (@maxLenght)

And how it should work on yours?

4

Re: Dynamic change of type

TaPaK wrote:

Valerii;
it is passed...
And how it should work on yours?

" Dynamic type change"
:-D

5

Re: Dynamic change of type

Rolg Hupin;

wrote:

"Dynamic change of type"

then dynamic sql

6

Re: Dynamic change of type

What for these knee-bends? The maximum length at   8000, declare it and do not suffer.

7

Re: Dynamic change of type

Hupin wrote:

it is passed...
As it is declared
@maxLenght?

int

As with an error in a word or without?
@maxLenght? @maxLength?

8

Re: Dynamic change of type

Valerii;
From what you solved that IT GENERALLY should work

9

Re: Dynamic change of type

Valerii;

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql wrote:

Variables can be used only in expressions, not in place of object names or keywords. To construct dynamic SQL statements, use EXECUTE.

10

Re: Dynamic change of type

TaPaK;
so thought why would be not present... ;-)
Nevertheless thanks... We will know...

11

Re: Dynamic change of type

And what for accurately to define length varchar? It all the same in storage takes a place exactly on length of the data it.

12

Re: Dynamic change of type

Sergey Alekseevich wrote:

What for these knee-bends? The maximum length at   8000, declare it and do not suffer.

Not to suffer at all: MAX also passed:-D

13

Re: Dynamic change of type

Alozar wrote:

And what for accurately to define length varchar? It all the same in storage takes a place exactly on length of the data it.

1. A designing question.
2. A question of indexes following from 1
3. If I am not mistaken the optimizer estimates how many pages it is necessary to lift proceeding from data types